问题
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.
here is my code
$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');
回答1:
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);
}
回答2:
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);
}
回答3:
You use imagecreatefrompng()
to load a picture, so you must make sure the picture format is png, not jpeg or other format. you can have a test use function imagecreatefromjpeg()
来源:https://stackoverflow.com/questions/24622948/creating-png-thumbnail-using-php