Solved

Resizing images with PHP - loss of quality?

Posted on 2004-10-23
461 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
Question by:spikeyjames
    4 Comments
     
    LVL 49

    Accepted Solution

    by:
    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
    >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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Suggested Solutions

    Title # Comments Views Activity
    Automatic Drupal Registration Part II 11 28
    cron job says it ran, no results 25 64
    Password reset token in url issue 13 16
    PHP Sctrapper 13 19
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now