Forum Moderators: coopster

Message Too Old, No Replies

GD resize discoloring image

         

pureform

6:08 am on Aug 18, 2005 (gmt 0)

10+ Year Member



I'm running this script on my server with GD version 2.0.28 [the one bundled with PHP 4.3.11], as well as a Linux server running [not sure of the specs there] ... and here's my problem:

I am uploading JPEG's, resizing them, and saving them to the server. My client was testing it when he uploaded a JPEG which resulted in a re-sized file with weird color changes. Blues were darker, oranges were darker, but white was the same, as well as black. The JPEG quality is set to 100.

While trying to debug this, I finally just passed the image through imagecreatefromjpeg() straight to imagejpeg() using imagecreatetruecolor() and it did the same thing:

# Content type was set above
$img = imagecreatefromjpeg("...");

$new_height = round(((260 * $height) / $width),0);
$new_img = imagecreatetruecolor(260,$new_height);

imagejpeg($new_img,"",100);
# Clear allocated memory on server
imagedestroy($new_img);
imagedestroy($img);

I even tried copying the pallet from the uploaded image to the new one, with no luck.

Here is the original code:

$filename = "uploads/images/" . $newID . "_ti.jpg";
$uploaded = move_uploaded_file($_FILES["TIfile"]["tmp_name"],$filename);

if (($_FILES["TIfile"]["type"]!= "image/pjpeg") && ($_FILES["TIfile"]["type"]!= "image/jpeg")) {
$errmsg = urlencode($_GLOBALS["msgs"]["mime_not_jpg"]);
header("Location: ...");
exit;
}

$imagehw = getimagesize($filename);

$width = $imagehw[0];
$height = $imagehw[1];

if ($width >= 260) {
$img = imagecreatefromjpeg($filename);

$new_height = round(((260 * $height) / $width),0);
$new_img = imagecreatetruecolor(260,$new_height);
imagecopyresampled($new_img,$img,0,0,0,0,260,$new_height,$width,$height);

# Save file to server
imagejpeg($new_img,$filename,100);

# Clear allocated memory on server
imagedestroy($new_img);
imagedestroy($img);
}
unlink("uploads/images/" . $iid . "_ti.jpg");
chmod($filename,0777);

bibby

6:21 am on Aug 18, 2005 (gmt 0)

10+ Year Member



have you tried NOT resampling or converting to true color and just leaving it as it were; no change at all?

I've had this problem with gifs, and it was fixed by reducing the number of palette colors.

pureform

6:25 am on Aug 18, 2005 (gmt 0)

10+ Year Member



Yeah, I tried that too ... tried it with imagecreate() instead of imagecreatetruecolor() as well.

bibby

7:01 am on Aug 18, 2005 (gmt 0)

10+ Year Member



Here's the "mini-maker" I'm currently using.
I didn't write it, and don't really get it, but it works.
Saves a thumbnail image to path ($dest_pict), from
filenames stashed in my DB.
It outputs some crazed text in my browser, but writes the files/. Colors look good though, so perhaps in here is an answer, even if I can't see it.
Good Luck

<?php

function miniature($pict, $dest_pict){

$handle = @imagecreatefromjpeg($pict);

$x=imagesx($handle);
$y=imagesy($handle);

if($x > $y){
$max = $x;
$min = $y;
}
if($x <= $y){
$max = $y;
$min = $x;
}

//$size_in_pixel : Size max of the label in pixel. The size of the picture being
//proportional to the original, this value define maximum size
//of largest side with dimensions of the picture. Sorry for my english!

//Here $size_in_pixel = 100 for a thumbnail.
$size_in_pixel = '100';

$rate = $max/$size_in_pixel;
$final_x = $x/$rate;
$final_y = $y/$rate;

if($final_x > $x) {
$final_x = $x;
$final_y = $y;
}

$final_x = ceil($final_x);
$final_y = ceil($final_y);

$black_picture = imageCreatetruecolor($final_x,$final_y);
imagefill($black_picture,0,0,imagecolorallocate($black_picture, 255, 255, 255));
imagecopyresampled($black_picture, $handle, 0, 0, 0, 0,$final_x, $final_y, $x, $y);

if(!@imagejpeg($black_picture,$dest_pict.'/mini_'.$pict, $size_in_pixel))
imagestring($black_picture, 1, $final_x-4, $final_y-8, ".", imagecolorallocate($black_picture,0,0,0));

//The number is the quality of the result picture
imagejpeg($black_picture,'', '100');
imagedestroy($handle);
imagedestroy($black_picture);
}

/////// end make-mini function

include ('../../######'); // Connects to My DB
$getall="select * from minis";
$getall=mysql_query($getall);

while ($tt = mysql_fetch_assoc($getall)){

$pict = "$tt[num]".'.jpg';
$dest_pict = " ###mypath### /photomini";

miniature($pict, $dest_pict);

}

?>