Resizing image don't work for every image - php

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;
}
?>

Related

PHP Image Gallery not working with Capital JPG extension in

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.

php image loses its quality after resize

I have a problem of image quality after a resize. Could you help me to find a solution to my problem?
This is a page where an image has been resized
goo.gl/5mYxxb , 460*275 and this the code that I am using to resize :
$newwidth = 460;
$newheight = 275;
$newwidth1 = 146;
$newheight2 = 88;
$temp = imagecreatetruecolor($newwidth, $newheight);
$black2 = imagecolorallocatealpha($temp,0,0,0,65);
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($path);
$src = imagecreatefromjpeg($path);
imagecopyresampled($temp, $src, 0,0,0,0, $newwidth, $newheight, $width, $height);
Imagettftext($temp, 16, 0, $start_x, $start_y, $black2, $font , "text");
imagejpeg($temp, $path,95);
imagedestroy($temp);
$temp2 = imagecreatetruecolor($newwidth1, $newheight2);
imagecopyresampled($temp2, $src, 0,0,0,0, $newwidth1, $newheight2, $width, $height);
Imagettftext($temp2, 8, 0, $start_x2, $start_y2, $black2, $font , "text");
imagejpeg($temp2,$pathFile ,90);
imagedestroy($temp2);
imagedestroy($src);

Create thumbnail after upload, PHP

I have implemented a file upload for pictures on my page, and tried to somehow generate thumbnails with the intention of clicking them to view via fancybox. The upload works but my function to create a thumbnail doesn't.
(This is included in my upload.php, right after "move_uploaded_file":
<?php
$src = $subdir.$fileupload['name'];
function make_thumb($src)
{
$source_image = imagecreatefromjpeg($src); //For testing purposes only jpeg now
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_width = 220;
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
header("Content-type: image/jpeg");
imagejpeg($virtual_image, realpath('./Thumbnails/filename.jpg')); //Temporary filename, will be changed
}
?>
Just FYI, this is an assignment and since I am a php beginner, I did use google, but can't find the problem in my case. Maybe my understanding of php is lacking too much.
Use this img_resize function, it is good for the most popular image formats
function img_resize($src, $dest, $width, $height, $rgb = 0xFFFFFF, $quality = 100)
{
if (!file_exists($src))
return false;
$size = getimagesize($src);
if ($size === false)
return false;
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$icfunc = "imagecreatefrom" . $format;
if (!function_exists($icfunc))
return false;
$x_ratio = $width / $size[0];
$y_ratio = $height / $size[1];
$ratio = min($x_ratio, $y_ratio);
$use_x_ratio = ($x_ratio == $ratio);
$new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
$new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
$new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
$new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
$isrc = $icfunc($src);
$idest = imagecreatetruecolor($width, $height);
imagefill($idest, 0, 0, $rgb);
if (($format == 'gif') or ($format == 'png')) {
imagealphablending($idest, false);
imagesavealpha($idest, true);
}
if ($format == 'gif') {
$transparent = imagecolorallocatealpha($idest, 255, 255, 255, 127);
imagefilledrectangle($idest, 0, 0, $width, $height, $transparent);
imagecolortransparent($idest, $transparent);
}
imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
getResultImage($idest, $dest, $size['mime']);
imagedestroy($isrc);
imagedestroy($idest);
return true;
}
function getResultImage($dst_r, $dest_path, $type)
{
switch ($type) {
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
return imagejpeg($dst_r, $dest_path, 90);
break;
case 'image/png';
return imagepng($dst_r, $dest_path, 2);
break;
case 'image/gif';
return imagegif($dst_r, $dest_path);
break;
default:
return;
}
}

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