SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Uploaded jpg is losing quality - why?

    I found a code long ago that I've been using for a while.
    Now I took the code to a new page and now it looks like the images are loosing some quality when I upload them.
    Earlier I used photos, but now I'm uploading text images and it's more obvious.

    The images I've uploaded are 270 px width, which is also what this script is making it after uploading, if the file is wider than that.
    But now I'm uploading images that are the right size.

    But is there something in this code that could be changed to keep the quality?

    Or should I do this in another better way?

    PHP Code:
    <? 
    // Width to reszie image to (in pixels) 
    $RESIZEWIDTH=270

    // Width to reszie image to (in pixels) 
    $RESIZEHEIGHT=2000

    // Enter the COMPLETE PATH to the directory where the thumb-nails should be stored WITH the final slash / 
    $thumbdir ='/the long address to where the files are stored/';

    // DO NOT EDIT BELOW HERE ----------------------------------------- 

    function ResizeImage($im,$maxwidth,$maxheight,$name

    global 
    $filename
    global 
    $thumbdir

    $name substr($name0strlen($name)-4); 

    $width imagesx($im); 
    $height imagesy($im); 

    if((
    $maxwidth && $width $maxwidth) || ($maxheight && $height $maxheight)){ 
    if(
    $maxwidth && $width $maxwidth){ 
    $widthratio $maxwidth/$width
    $RESIZEWIDTH=true

    if(
    $maxheight && $height $maxheight){ 
    $heightratio $maxheight/$height
    $RESIZEHEIGHT=true

    if(
    $RESIZEWIDTH && $RESIZEHEIGHT){ 
    if(
    $widthratio $heightratio){ 
    $ratio $widthratio
    }else{ 
    $ratio $heightratio

    }elseif(
    $RESIZEWIDTH){ 
    $ratio $widthratio
    }elseif(
    $RESIZEHEIGHT){ 
    $ratio $heightratio

    $newwidth $width $ratio
    $newheight $height $ratio
    if(
    function_exists("imagecopyresampled")){ 
    $newim imagecreatetruecolor($newwidth$newheight); 
    imagecopyresampled($newim$im0000$newwidth$newheight$width$height); 
    }else{ 
    $newim imagecreate($newwidth$newheight); 
    imagecopyresized($newim$im0000$newwidth$newheight$width$height); 

    ImageJpeg ($newim$thumbdir $name ".jpg");
    ImageDestroy ($newim); 
    } else { 
    ImageJpeg ($im$thumbdir $name ".jpg"); 


    $filename $name;
    $filnamn $name '.jpg';
    mysql_query("INSERT INTO the_db_name (prodnav_namn) VALUES ('$filnamn')");



    if (
    $_FILES['image']['size']) 

    if(
    $_FILES['image']['type'] == "image/pjpeg" || $_FILES['image']['type'] == "image/jpeg"){ 
    $im imagecreatefromjpeg($_FILES['image']['tmp_name']); 
    }elseif(
    $_FILES['image']['type'] == "image/x-png" || $_FILES['image']['type'] == "image/png"){ 
    $im imagecreatefrompng($_FILES['image']['tmp_name']); 
    }elseif(
    $_FILES['image']['type'] == "image/gif"){ 
    $im imagecreatefromgif($_FILES['image']['tmp_name']); 


    if (
    $im

    if (
    file_exists("$thumbdir/{$_FILES['image']['tmp_name']}")) 

    unlink("$thumbdir/{$_FILES['image']['tmp_name']}"); 

    ResizeImage($im$RESIZEWIDTH$RESIZEHEIGHT$_FILES['image']['name']); 
    ImageDestroy ($im); 



    ?>
    And the form looks like this:

    PHP Code:
    <form method="post" enctype="multipart/form-data" class="valbar"
      <
    div align="center">
        <
    p>&nbsp;</p>
        <
    p><class="bread">Image Format</b><br>
          <
    input type="file" name="image" size="50">
            <
    br>
            <
    br
            <
    input type="submit" value="Fix!"
          </
    p>
        <
    p>&nbsp;</p>
      </
    div>
    </
    form

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The default quality value for the PHP-function imagejpeg is 75, set it it to 100.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Where in the code would that part be then and how to write it?
    Since I didn't write this one myself...

    :-(

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Wild guess : imagejpeg()

    Did you know you can search the man with www.php.net/<func_name> ?

    eg www.php.net/imagejpeg

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question

    So, where in my code should I put this part to make it work?

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    the parameters for imagejpeg are:
    Code:
    bool imagejpeg  ( resource $image  [, string $filename  [, int $quality  ]] )
    so....
    PHP Code:
    ImageJpeg ($newim$thumbdir $name ".jpg"100); 
    with 100 being the quality
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Something is wrong. I did put that code in my file, but still get the same bad image after uploading.

    I've attached the original file and what it looks like after uploading.
    Attached Images Attached Images

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,394
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I think text never resizes very well.

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, theere isn't any resize in this file. It's actually the original size in this test. That's what's bothering me...

    Maybe I should try to find a script that is just uploading a file.
    And let the administrator work with his files in Photoshop in the right size.
    Does anyone have a tip on where to find a very simple upload script?

  10. #10
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only the red text are changed in two attached images, and even though, it appears to me a color change. I can't say there's any quality difference between the two images.
    Imagination is more important than knowledge. - Einstein

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But it looks a lite cheap to use on a site for a person who wants to show his graphics work, which is what my friend wants to do on his site.

  12. #12
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The problem with your code seems to be that it recompresses each jpg image you upload while what you probably want is simply copy the uploaded image to the server without modifications. I don't know in detail what this script is supposed to do with the images but I suppose it might resize them if they are too large but even if they aren't they are recompressed. I suggest you find/write a different code which does what you want.

    BTW, if you use imagejpeg() function you will never be able to achieve very good jpg quality even if you set it to 100 - see your example, the red letters went pale. The problem is that this function always applies colour pixel sub-sampling compression which will always cause thin lines of intense colour to lose saturation. That's why I almost never use imagejpeg (gd library), instead I use ImageMagick so I can achieve very good quality jpegs - there I can control both the image (details) quality and pixel sub-sampling.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •