negative resized image - php

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.

Related

Resize, rename and upload image with PHP

I would like to resize, rename and upload an image with PHP.
So my working script is the following:
$file_name = $_FILES['HOT_Logo']['name'];
$file_tmp_name = $_FILES['HOT_Logo']['tmp_name'];
$file_target = '../../images/hotel-logos/';
$file_size = $_FILES['HOT_Logo']['size'];
// Resize
$ratio = $width/$height;
if($ratio > 1) {
$new_width = 300;
$new_height = 300/$ratio;
}
else {
$new_width = 300*$ratio;
$new_height = 300;
}
$src = imagecreatefromstring(file_get_contents($file_tmp_name));
$dst = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($src);
imagepng($dst, $file_target.$file_name);
imagedestroy($dst);
// Rename file
$temp = explode('.', $_FILES['HOT_Logo']['name']);
$newfilename = 'new_img_name.'.end($temp);
// Upload image
if(move_uploaded_file($_FILES['HOT_Logo']['tmp_name'], $file_target.$newfilename)) {
...
}
Problem with this script is it upload two image:
The renamed image but unresized.
The non renamed image but resized.
Why ?
You are creating a image, then moving the uploaded file, which is why it's creating two files. I believe your code is supposed to be:
$file_name = $_FILES['HOT_Logo']['name'];
$file_tmp_name = $_FILES['HOT_Logo']['tmp_name'];
$file_target = '../../images/hotel-logos/';
$file_size = $_FILES['HOT_Logo']['size'];
// Resize
$ratio = $width/$height;
if($ratio > 1) {
$new_width = 300;
$new_height = 300/$ratio;
}
else {
$new_width = 300*$ratio;
$new_height = 300;
}
// Rename file
$temp = explode('.', $file_name);
$newfilename = 'new_img_name.'.end($temp);
// Upload image
if(move_uploaded_file($file_tmp_name , $file_target.$newfilename)) {
$src = imagecreatefromstring(file_get_contents($file_target.$newfilename));
$dst = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($src);
imagepng($dst, $file_target.$newfilename);
imagedestroy($dst);
....
}

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

Image resizing not working?

I used to do this by percentages although now I am looking to do it by Pixels instead, although after trial, the images don't seem to be resizing? And I am not sure why?
$get_resize_width = '225';
$get_resize_height = '150';
$info = getimagesize($file);
list($width, $height) = getimagesize($file);
$new_width = $get_resize_width;
$new_height = $get_resize_height;
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($file);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($file);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($file);
$image = imagecreatetruecolor($new_width, $new_height);
$photo = imagecreatefromjpeg($file);
imagecopyresampled($image, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image, $newfile, $img_quality);
$origional_image = $file;
$compressed_image = $newfile;
I have tried adding px to the end of the variables although this doesn't seem to make a difference?

Image resizing not working (using base64)

The following code is supposed to output a resized image in a string. Unfortunately it only outputs MQ== so where is my mistake? Help is really appreciated :)
<?php
// The file
$filename = 'tick.png';
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$newImage = imagepng($image_p, null, 100); // got in has to be from 0 to 9
echo base64_encode(file_get_contents($newImage)); // this still does not work°
?>
Compression level can't be 100. Only from 0 to 9.
imagepng($image_p, null, from 0 to 9);
Base64 encode you can do with:
ob_start();
imagepng($image_p, null, 9); // got in has to be from 0 to 9
$stream = ob_get_clean();
echo base64_encode($stream);
This is how i do it:
$Temp_Dump = $_FILES["file"]["tmp_name"];
// Get new sizes
list($width, $height) = getimagesize($Temp_Dump);
$newwidth = 90;
$newheight = 90;
// Load
$Temp_thumb = imagecreatetruecolor($newwidth, $newheight);
//$source = imagecreatefromjpeg($Temp_Dump);
if($extension == "jpg" OR $extension=='jpeg'){
$source = ImageCreateFromJpeg($Temp_Dump);
}elseif ($extension == "gif"){
$source = ImageCreateFromGIF($Temp_Dump);
}elseif ($extension == 'png'){
$source = imageCreateFromPNG($Temp_Dump);
}
// Resize
imagecopyresized($Temp_thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

download image from remote source and resize then save

Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.

Categories