PHP Image Gallery not working with Capital JPG extension in - php

PHP image Gallery not working with Capital JPG extension, it shows an error -- 'Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 23040 bytes)'-- But the other all images are working fine. I am using Uber gallery
I think this part of code affects..
$newImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
// Create new thumbnail
if ($imgInfo[2] == IMAGETYPE_JPEG) {
$image = imagecreatefromjpeg($source);
imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbWidth, $thumbHeight, $width, $height);
imagejpeg($newImage, $destination, $quality);
} elseif ($imgInfo[2] == IMAGETYPE_GIF) {
$image = imagecreatefromgif($source);
imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbWidth, $thumbHeight, $width, $height);
imagegif($newImage, $destination);
} elseif ($imgInfo[2] == IMAGETYPE_PNG) {
$image = imagecreatefrompng($source);
imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbWidth, $thumbHeight, $width, $height);
imagepng($newImage, $destination);
}
// Return relative path to thumbnail
$relativePath = $this->_rThumbsDir . '/' . $fileName;
return $relativePath;
}
How can I fix this issue.

Related

Resizing image don't work for every image

When I am doing resizing and converting picture to jpeg/jpg it isnt working for every PNG and JPG file, and I dont why? File isnt uploaded into server but information of path is stored into database.
This is code for resizing (also for png I am doing filling with white instead of transparency). I am using GD module not Imagick because I dont have Imagick installed on server.
function imagecreatefromfile( $filename ) {
if (!file_exists($filename)) {
throw new InvalidArgumentException('File "'.$filename.'" not found.');
}
switch ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ))) {
case 'jpeg':
case 'jpg':
return imagecreatefromjpeg($filename);
break;
case 'png':
return imagecreatefrompng($filename);
break;
default:
throw new InvalidArgumentException('File "'.$filename.'" is not valid jpg or png.');
break;
}
}
/* do resizing of picture */
$src = imagecreatefromfile($target);
$path1 = strtolower( pathinfo( $target, PATHINFO_EXTENSION ));
list ($width, $height) = getimagesize($target);
$newwidth = 300;
if($heigth > $width){
$newheight = ($height/$width)*$newwidth;
}
else if($width == $height){
$newheight = 300;
}
else if($width > $height){
$newheight = ($width/$height)*$newwidth;
}
unlink($target);
switch ($path1)
{
case 'jpg':
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($tmp, $target, 90);
imagedestroy($src);
imagedestroy($tmp);
break;
case 'png':
$backgroundImg = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, 255, 255, 255);
imagefill($backgroundImg, 0, 0, $color);
imagecopy($backgroundImg, $src, 0, 0, 0, 0, $width, $height);
$tmp2 = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp2, $backgroundImg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($tmp2, $target, 90);
imagedestroy($src);
imagedestroy($backgroundImg);
imagedestroy($tmp2);
break;
}
?>

Server returns error 500 on JPG GD image

My webapp is ajaxing an image on server. If the image size is lower than a defined value the script works as expected. If greater, the image is resized with GD. It works fine with PNG and GIF files but returns an Error 500 with JPG images.... Where am I wrong ?
if (strtolower($e=='.jpg') || strtolower($e=='.jpeg')) {
header('Content-type; image/jpeg');
$gd = imagecreatetruecolor($w, $h);
$image = imagecreatefromjpeg($img);
imagecopyresampled($gd, $image, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagejpeg($gd,$img_temp,100);
}
if (strtolower($e=='.png')) {
header('Content-Type: image/png');
$gd = imagecreatetruecolor($w, $h);
$image = imagecreatefrompng($img);
imagecopyresampled($gd, $image, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagepng($gd,$img_temp,9);
}
if (strtolower($e=='.gif')) {
header('Content-type; image/gif');
$gd = imagecreatetruecolor($w, $h);
$image = imagecreatefromgif($img);
imagecopyresampled($gd, $image, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagegif($gd,$img_temp);
}

How to create retina image from JPEG with PHP

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);

negative resized image

this is my script below which i use to re-size the images. My problem is that this script generates negative images (like negative films [with only .png files]) . Where/What is the problem ?
I used GD library to re-size the images but I got same result.
$dir = "../images/sliderimages/";
$photo = $_FILES['slid_image_upload']['name'];
$tmp_name = $_FILES['slid_image_upload']['tmp_name'];
$filename = $dir.$photo;
$dir_thm = "../images/thm_sliderimages/";
$thm_filename = $dir_thm.'thm_'.$photo;
/************Resizing the image***************/
$size = getimagesize($tmp_name);
$width = $size[0];
$height = $size[1];
$newheight = 200;
$newwidth = 420;
$newheight_thm = 50;
$newwidth_thm = 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp_thm=imagecreatetruecolor($newwidth_thm, $newheight_thm);
if($size[2] == IMAGETYPE_GIF)
{
$src = imagecreatefromgif($tmp_name);
imagecopyresampled($tmp,$src, 0,0,0,0, $newwidth, $newheight, $width, $height);
imagecopyresampled($tmp_thm, $src, 0,0,0,0, $newwidth_thm, $newheight_thm, $width, $height);
imagegif($tmp,$filename,100);
imagegif($tmp_thm,$thm_filename,100);
}
elseif($size[2] == IMAGETYPE_JPEG)
{
$src = imagecreatefromjpeg($tmp_name);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp_thm, $src, 0,0,0,0, $newwidth_thm, $newheight_thm, $width, $height);
imagejpeg($tmp,$filename,100);
imagejpeg($tmp_thm,$thm_filename,100);
}
elseif($size[2] == IMAGETYPE_PNG)
{
$src = imagecreatefrompng($tmp_name);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp_thm, $src, 0,0,0,0, $newwidth_thm, $newheight_thm, $width, $height);
imagepng($tmp,$filename,9);
imagepng($tmp_thm,$thm_filename,9);
}
imagedestroy($src);
imagedestroy($tmp);
I'd advice you to try you code on another server\local machine to became sure that it's not current library installation issues.

Resize Image PNG With transparence

I want to resize an image PNG with transparence plz help.
Here is the code :
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
Try this
UPDATED
function createThumb($upfile, $dstfile, $max_width, $max_height){
$size = getimagesize($upfile);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height)) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if($size['mime'] == "image/jpeg"){
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imageinterlace( $dst, true);
ImageJpeg($dst, $dstfile, 100);
} else if ($size['mime'] == "image/png"){
$src = ImageCreateFrompng($upfile);
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($src, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($src, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($src, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($src, true);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
Imagepng($dst, $dstfile);
} else {
$src = ImageCreateFromGif($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imagegif($dst, $dstfile);
}
}
There a simple to use, open source library called PHP Image Magician. It uses GD and supports transparency
Example of basis usage:
$magicianObj = new imageLib('racecar.png');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');

Categories