spikeyjames
asked on
Resizing images with PHP - loss of quality?
Hi, I'm using this code to resize images in 'main' directory and place them in 'tumb' directoy:
<?
$new_w = 200;
$cfg_fullsizepics_path = "main";
$cfg_thumb_path = "thumb";
$filepath = "main";
$dir = dir($filepath);
while($entry=$dir->read()) {
if($entry == "." || $entry == "..") {
continue;
}
$fp = @fopen("$filepath/$entry", "r");
$photo_filename = "$entry";
$image_stats = GetImageSize($cfg_fullsize pics_path. "/".$entry );
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
if (!file_exists($cfg_thumb_p ath."/".$e ntry)) {
if ($img_type=="2") {
$src_img = imagecreatefromjpeg($cfg_f ullsizepic s_path."/" .$entry);
$dst_img = imagecreate($new_w,$new_h) ;
imagecopyresized($dst_img, $src_img,0 ,0,0,0,$ne w_w,$new_h ,imagesx($ src_img),i magesy($sr c_img));
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry ");
} elseif ($img_type=="3") {
$dst_img=ImageCreate($new_ w,$new_h);
$src_img=ImageCreateFrompn g($cfg_ful lsizepics_ path."/".$ entry);
ImageCopyResized($dst_img, $src_img,0 ,0,0,0,$ne w_w,$new_h ,ImageSX($ src_img),I mageSY($sr c_img));
Imagepng($dst_img, "$cfg_thumb_path"."/$entry ");
} elseif ($img_type=="1") {
$cfg_thumb_url=$cfg_fullsi zepics_url ;
}
}
echo "$entry";
echo "
";
}
?>
It works well, BUT the images that are resized come out with what looks like 256 colours with a blue tint. Can anyone tell me how to resize images but preserver the colour quality in php?
BTW, I can't use GD library as i don't have permissions to install it :(
thanks.
<?
$new_w = 200;
$cfg_fullsizepics_path = "main";
$cfg_thumb_path = "thumb";
$filepath = "main";
$dir = dir($filepath);
while($entry=$dir->read())
if($entry == "." || $entry == "..") {
continue;
}
$fp = @fopen("$filepath/$entry",
$photo_filename = "$entry";
$image_stats = GetImageSize($cfg_fullsize
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
if (!file_exists($cfg_thumb_p
if ($img_type=="2") {
$src_img = imagecreatefromjpeg($cfg_f
$dst_img = imagecreate($new_w,$new_h)
imagecopyresized($dst_img,
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry
} elseif ($img_type=="3") {
$dst_img=ImageCreate($new_
$src_img=ImageCreateFrompn
ImageCopyResized($dst_img,
Imagepng($dst_img, "$cfg_thumb_path"."/$entry
} elseif ($img_type=="1") {
$cfg_thumb_url=$cfg_fullsi
}
}
echo "$entry";
echo "
";
}
?>
It works well, BUT the images that are resized come out with what looks like 256 colours with a blue tint. Can anyone tell me how to resize images but preserver the colour quality in php?
BTW, I can't use GD library as i don't have permissions to install it :(
thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much!!!
Such a simple solution! sigh.
I'm not sure if i'm using GD lib?...maybe my hosts have it installed after all...how do I check?
Such a simple solution! sigh.
I'm not sure if i'm using GD lib?...maybe my hosts have it installed after all...how do I check?
php_info (or was it phpinfo()?)
At least, one of the two shows info on what modules are installed at your server.
<?php phpinfo(); ?>
Regards
-r-
At least, one of the two shows info on what modules are installed at your server.
<?php phpinfo(); ?>
Regards
-r-
You ARE using GD lib, aren't you? Even imagecreate is a GD function. to my knowledge at least.
Regards
-r-