I have a script that resizes uploaded images. It works fine for PNGs and JPGs but not GIFs. For the GIFs, it's supposed to convert them into JPGs and then resize them. The conversion works, but then they fail to be resized...
function resize_image($file, $maxWidth, $maxHeight) {
$jpgFile = substr_replace($file, 'jpeg', -3);
$fileType = strtolower(substr($file, -3));
...
if ($fileType == 'gif') {
$test = imagecreatefromgif($file);
imagejpeg($test, $jpgFile);
$src = imagecreatefromjpeg($jpgFile);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($dst, $jpgfile);
}
}
I don't think you need to output the image after it's created from a gif - imagecreatefromgif reads an image into memory, you should be able to do this:
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($dst, $jpgfile);
What version of the GD library are you using? According to the official PHP documentation:
GIF support was removed from the GD library in Version 1.6, and added
back in Version 2.0.28. This function is not available between these
versions.
I ended up bypassing the conversion of GIF to JPG, and resized the GIF directly. However, to maintain the transparency (by default it turns the transparent background to black, which is the reason I originally converted it to JPG first before resizing), I had to add in a few instructions.
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecolortransparent($dst, imagecolorallocatealpha($dst, 0, 0, 0, 127));
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($dst, $file);
Related
Borrowed this code to circle crop an image output.
// create the transparent circle image
$filename = APP_WEB_PATH."img/user/".$_GET['img'].".jpg";
$imagefilenamepng = APP_WEB_PATH."img/user/".$_GET['img']."_c.png";
$image_s = imagecreatefromstring(file_get_contents($filename));
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 300;
$newheight = 300;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);
imagepng($image,$imagefilenamepng);
imagedestroy($image);
imagedestroy($mask);
The image looks good and transparent and even when opened with a photo editor
but when used inside an image using php GDI i get a black border instead of transparent
what am I missing here.. (if I use paint.net image editor and re-save the image, then it works fine..) the below image works fine after saving in the photo editor
I use the code to call it as
self::$im = imagecreatefromjpeg($imgname1);
$im2 = imagecreatefrompng($imgname2);
$sx = imagesx($im2)*$scale;
$sy = imagesy($im2)*$scale;
$stamp = imagescale($im2, $sx);
imagecopy(self::$im, $stamp, $xcord, $ycord, 0, 0, $sx, $sy);
here self::$im is the larger image, and $im2 is the one that is cropped
I've a png image uploading script in my website and i'm scaling down the large images to small size and i'm using this code but it loses the transparent background
$image = $tmp; // Uploaded Image
$maxImgWidth = 224;
$src = imagecreatefrompng($image);
list($width, $height) = getimagesize($image);
$newwidth = $maxImgWidth;
$newheight = ($height / $width) * $newwidth;
$newImage = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage, "../thumb/$name_after-$code.$ext", 1);
imagedestroy($src);
imagedestroy($newImage);
This code makes the image without transparency :
And i want with transparent background like this one :
Sorry I have no experience in PHP but after I read this page, I think you may need add the transparent colour after you set the save alpha is true
imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $color);
if still cannot try add this after imagecolorallocatealpha(),
imagecolortransparent($image, $color);
A few months ago i wrote the following script to convert an uploaded image with PHP to Retina and non retina images. The iphone app that was working with this script only used PNG images, so i wrote the script to work with PNG's.
$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.png', '_retina.png', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));
$image_info = getimagesize($filename);
$image = imagecreatefrompng($filename);
$width = imagesx($image);
$height = imagesy($image);
$new_width = $width/2.0;
$new_height = $height/2.0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$new_filename = str_replace('_retina.png', '.png', $filename);
imagepng($new_image, $new_filename);
Now i need the same script but then to be used with Jpeg images. Because the iphone app will load images with a higher resolution we chose Jpeg. But i can't figure out how to make that work.
What i've tried so far:
Replacing imagecreatefrompng with the jpeg version
Replacing imagepng with the jpeg version
Does anybody have a working example or useful link that can set me to the right direction?
I figured out what the problem was about. I assumed jpg php functions could not handle the transparency, so i removed those lines and forgot about them. Apparently it just creates a white background and it does not fail. So the script is as follows:
$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.jpg', '_retina.jpg', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));
$image_info = getimagesize($filename);
$image = imagecreatefromjpeg($filename);
$width = imagesx($image);
$height = imagesy($image);
$new_width = $width/2.0;
$new_height = $height/2.0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$new_filename = str_replace('_retina.jpg', '.jpg', $filename);
imagejpeg($new_image, $new_filename);
The following code works on my laptop but doesn't work on remote server:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$filename = "../../content/".base64_decode($_GET["file"]);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext=="jpg" || $ext=="jpeg") {
$image_s = imagecreatefromjpeg($filename);
} else if ($ext=="png") {
$image_s = imagecreatefrompng($filename);
}
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 285;
$newheight = 232;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image,true);
imagecopyresampled($image,$image_s,0,0,0,0,$newwidth,$newheight,$width,$height);
// create masking
$mask = imagecreatetruecolor($width, $height);
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask, $transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopy($image, $mask, 0, 0, 0, 0, $newwidth, $newheight);
imagecolortransparent($image, $red);
imagefill($image,0,0, $red);
// output and free memory
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($mask);
?>
It shows the following image on my laptop:
http://i.stack.imgur.com/Aai1r.png
And this is how it's shown on remote server:
http://i.stack.imgur.com/77fbV.png
What do you think? What's a problem?
Changed the line:
imagecopy($image, $mask, 0, 0, 0, 0, $newwidth, $newheight);
to:
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight,100);
I think a basic reading of how to debug http://blog.regehr.org/archives/199 would be useful to you, and help you to solve your problem
I think you might have problems with filepath. Check if pathinfo returns valid extension and add after $filename declaration something to check if it exists and is readable
if(!is_file($file) || !is_readable($file)){ die('Readable file not found');}
I'm trying to resize an image with GD and am seeing a color loss on the resized image. Here is my code:
$src = imagecreatefromstring(file_get_contents($source));
ImageCopyResized($dst, $src, 0, 0, 0, 0, $t_width, $t_height, ImageSX($src), ImageSY($src));
Imagejpeg($dst, $dest, 90);
Are you using imagecreatetruecolor when declaring $dst?
The right way to do this is:
$dst = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($dst, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);