Resizing images with PHP - loss of quality?

Posted on 2004-10-23
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 == "..") {
    $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);
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry");
 } elseif  ($img_type=="3") {
 Imagepng($dst_img, "$cfg_thumb_path"."/$entry");
 } elseif  ($img_type=="1") {
  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 :(
Question by:spikeyjames
    LVL 49

    Accepted Solution

    change imagecreate() into imagecreatetruecolor()

    See also:
    > We recommend the use of imagecreatetruecolor().


    LVL 49

    Expert Comment

    >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.


    LVL 1

    Author Comment

    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 do I check?
    LVL 49

    Expert Comment

    php_info (or was it phpinfo()?)

    At least, one of the two shows info on what modules are installed at your server.

    <?php phpinfo(); ?>



    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