使用PHP创建PNG缩略图 [英] Creating PNG thumbnail using PHP
本文介绍了使用PHP创建PNG缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个缩略图图像,它返回了一个错误,该imagecopyresize()期望将2参数用作资源,但是它可以创建该图像,但输出只是一个小的黑色图像.
I'm trying to create a thumbnail image it returned an error, that imagecopyresized() expects 2 parameter to be resource, but it can create the image but the output is only a small black image.
这是我的代码
$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透明性.
Here's one function that does it. Uses assist function for PNG transparency.
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);
}
这篇关于使用PHP创建PNG缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文