[英]Creating PNG thumbnail using PHP
我正在嘗試創建一個返回錯誤的縮略圖圖像,該 imagecopyresized() 期望 2 參數是資源,但它可以創建圖像但輸出只是一個小的黑色圖像。
這是我的代碼
$image = "asd.PNG";
$image_size = getimagesize($image);
$image_width = $image_size[0];
$image_height = $image_size[1];
$new_size = ($image_width + $image_height)/($image_width*($image_height/45));
$new_width = $image_width * $new_size;
$new_height = $image_height * $new_size;
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_mage = imagecreatefrompng($image);
imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagepng($new_image, $image.'.thumb.png');
這是一個可以做到這一點的函數。 使用輔助功能實現 PNG 透明度。
public function redimesionImage($endThu,$newX,$newY,$endImg,$fileType){
// Copy the image to be resized
copy($endThu, $endImg);
// Retrieves the image data
list($width, $height) = getimagesize($endImg);
// If the width is greater ...
if($width >= $height) {
// I set the width of the image to the desired size ...
$newXimage = $newX;
// And calculate the size of the time to not stretch the image
$newYimage = ($height / $width) * $newXimage;
} else {
// Define the desired height ...
$newYimage = $newY;
// And calculate the width to not stretch the image
$newXimage = ($width / $height) * $newYimage;
}
// Creates an initial image in memory with calculated measures
$imageInicial = imagecreatetruecolor(ceil($newXimage), ceil($newYimage));
// I check the extension of the image and create their respective image
if ($fileType == 'jpeg') $endereco = imagecreatefromjpeg($endImg);
if ($fileType == 'jpg') $endereco = imagecreatefromjpeg($endImg);
if ($fileType == 'png') {
$endereco = imagecreatefrompng($endImg);
imagealphablending($imageInicial, false);
imagesavealpha($imageInicial,true);
$transparent = imagecolorallocatealpha($imageInicial, 255, 255, 255, 127);
imagefilledrectangle($endereco, 0, 0, $newXimage, $newYimage, $transparent);
}
if ($fileType == 'gif') {
$endereco = imagecreatefromgif($endImg);
$this->setTransparency($imageInicial,$endereco);
}
// I merge the image to be resized with created in memory
imagecopyresampled($imageInicial, $endereco, 0, 0, 0, 0, ceil($newXimage), ceil($newYimage), ceil($width), ceil($height));
// Creates the image in its final lacal, according to its extension
if ($fileType == 'jpeg') imagejpeg($imageInicial, $endImg, 100);
if ($fileType == 'jpg') imagejpeg($imageInicial, $endImg, 100);
if ($fileType == 'png') imagepng($imageInicial, $endImg, 9);
if ($fileType == 'gif') imagegif($imageInicial, $endImg, 100);
}
// Function to assist the PNG images, sets the transparency in the image
private function setTransparency($new_image,$image_source){
$transparencyIndex = imagecolortransparent($image_source);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
if($transparencyIndex >= 0){
$transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);
}
$transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($new_image, 0, 0, $transparencyIndex);
imagecolortransparent($new_image, $transparencyIndex);
}
public function redimesionImage($endThu,$newX,$newY,$endImg,$fileType){
copy($endThu, $endImg);
list($width, $height) = getimagesize($endImg);
if($width >= $height) {
$newXimage = $newX;
$newYimage = ($height / $width) * $newXimage;
} else {
$newYimage = $newY;
$newXimage = ($width / $height) * $newYimage;
}
$imageInicial = imagecreatetruecolor(ceil($newXimage), ceil($newYimage));
if ($fileType == 'jpeg') $endereco = imagecreatefromjpeg($endImg);
if ($fileType == 'jpg') $endereco = imagecreatefromjpeg($endImg);
if ($fileType == 'png') {
$endereco = imagecreatefrompng($endImg);
imagealphablending($imageInicial, false);
imagesavealpha($imageInicial,true);
$transparent = imagecolorallocatealpha($imageInicial, 255, 255, 255, 127);
imagefilledrectangle($endereco, 0, 0, $newXimage, $newYimage, $transparent);
}
if ($fileType == 'gif') {
$endereco = imagecreatefromgif($endImg);
$this->setTransparency($imageInicial,$endereco);
}
imagecopyresampled($imageInicial, $endereco, 0, 0, 0, 0, ceil($newXimage), ceil($newYimage), ceil($width), ceil($height));
if ($fileType == 'jpeg') imagejpeg($imageInicial, $endImg, 100);
if ($fileType == 'jpg') imagejpeg($imageInicial, $endImg, 100);
if ($fileType == 'png') imagepng($imageInicial, $endImg, 9);
if ($fileType == 'gif') imagegif($imageInicial, $endImg, 100);
}
private function setTransparency($new_image,$image_source){
$transparencyIndex = imagecolortransparent($image_source);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
if($transparencyIndex >= 0){
$transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);
}
$transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($new_image, 0, 0, $transparencyIndex);
imagecolortransparent($new_image, $transparencyIndex);
}
您使用imagecreatefrompng()
加載圖片,因此您必須確保圖片格式是 png,而不是 jpeg 或其他格式。 你可以有一個測試使用函數imagecreatefromjpeg()
以下是創建縮略圖的兩種不同方法。
第一種方式
function createThumbnail($filepath, $thumbpath, $thumbnail_width, $thumbnail_height, $background=false) {
list($original_width, $original_height, $original_type) = getimagesize($filepath);
if ($original_width > $original_height) {
$new_width = $thumbnail_width;
$new_height = intval($original_height * $new_width / $original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width * $new_height / $original_height);
}
$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);
if ($original_type === 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
} else if ($original_type === 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
} else if ($original_type === 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
} else {
return false;
}
$old_image = $imgcreatefrom($filepath);
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); // creates new image, but with a black background
// figuring out the color for the background
if($original_type == 1 || $original_type == 2) {
list($red, $green, $blue) = $background;
$color = imagecolorallocate($new_image, $red, $green, $blue);
imagefill($new_image, 0, 0, $color);
// apply transparent background only if is a png image
} else if($original_type == 3){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
//imagesavealpha($new_image, TRUE);
//$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
//imagefill($new_image, 0, 0, $color);
}
imagecopyresampled($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
$imgt($new_image, $thumbpath);
return file_exists($thumbpath);
}
其他方式:
function createThumbnails($filepath, $thumbpath, $thumbnail_height, $background=false) {
list($width, $height, $original_type) = getimagesize($filepath);
$new_width = floor( $width * ( $thumbnail_height / $height ) );
$new_height = $thumbnail_height;
if ($original_type === 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
} else if ($original_type === 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
} else if ($original_type === 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
} else {
return false;
}
$old_image = $imgcreatefrom($filepath);
$new_image = imagecreatetruecolor($new_width, $new_height); // creates new image, but with a black background
// figuring out the color for the background
if($original_type == 1 || $original_type == 2) {
list($red, $green, $blue) = $background;
$color = imagecolorallocate($new_image, $red, $green, $blue);
imagefill($new_image, 0, 0, $color);
// apply transparent background only if is a png image
} else if($original_type == 3){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
//imagesavealpha($new_image, TRUE);
//$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
//imagefill($new_image, 0, 0, $color);
}
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$imgt($new_image, $thumbpath);
return file_exists($thumbpath);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.