[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Resizing images with PHP - loss of quality?

Posted on 2004-10-23
4
Medium Priority
?
466 Views
Last Modified: 2008-02-01
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_fullsizepics_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_path."/".$entry))  {
 if ($img_type=="2") {
 $src_img = imagecreatefromjpeg($cfg_fullsizepics_path."/".$entry);
 $dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry");
 } elseif  ($img_type=="3") {
 $dst_img=ImageCreate($new_w,$new_h);
 $src_img=ImageCreateFrompng($cfg_fullsizepics_path."/".$entry);
ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
 Imagepng($dst_img, "$cfg_thumb_path"."/$entry");
 } elseif  ($img_type=="1") {
 $cfg_thumb_url=$cfg_fullsizepics_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.
0
Comment
Question by:spikeyjames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 1000 total points
ID: 12387628
change imagecreate() into imagecreatetruecolor()

See also: http://www.php.net/imagecreate
> We recommend the use of imagecreatetruecolor().

Regards

-r-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12387632
>BTW, I can't use GD library as i don't have permissions to install it :(

You ARE using GD lib, aren't you? Even imagecreate is a GD function. to my knowledge at least.

Regards

-r-
0
 
LVL 1

Author Comment

by:spikeyjames
ID: 12387765
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?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12387769
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-
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question