SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image Auto-Scale

    I have been working on a website for a relative of mine, and it includes a Gallery that allows him to upload images and the like. But, since the images he's uploading are from his 10MP Camera, the images are huge (both in size, and in dimensions). The image size doesn't bother me, but the dimensions messes up the page, since the images are something like 2000x2000px. What I would like to do would be to keep a consistent size on all the images, so I would like to scale down the images after they're uploaded. Any ideas on how to do this? Thanks.

    Note: The images will need to remain high quality after scaling, especially some of them are pictures of Paintings he's made.
    MattsBlog.Ca
    LiveMP -- Coming soon!

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I wrote this script to make thumbnails, which is pretty much the same as what you want to do. It outputs in PNG, so the quality should still be good.
    PHP Code:
    function resize($img$thumbmaxheight$thumbmaxwidth) {
          
    $imgsize getimagesize($img) or die("$img is not a valid image");
          list(
    $width$height) = $imgsize;
          if (
    $thumbmaxwidth $width && $width $height) {
            
    $thumbwidth $thumbmaxwidth;
            
    $thumbheight = ($thumbwidth $width) * $height;
          }
          elseif (
    $thumbmaxheight $height && $height $width) {
            
    $thumbheight $thumbmaxheight;
            
    $thumbwidth = ($thumbheight /$height) * $width;
          }
          else {
            return 
    'No resizing necessary';
          }
          
    $imgbuffer imagecreatetruecolor($thumbwidth$thumbheight);
          if (
    $imgsize['mime'] == "image/jpeg") {
            
    $image imagecreatefromjpeg("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/gif") {
            
    $image imagecreatefromgif("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/png") {
            
    $image imagecreatefrompng("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/bmp") {
            
    $image imagecreatefromwbmp("$album/$img");
          }
          if (!
    $image) {return false;} else {
            
    imagecopyresampled($imgbuffer$image0000$thumbwidth$thumbheight$width$height);
            
    $imgoutput imagepng($imgbuffer"location/for/newimage.png"4);
            
    imagedestroy($imgbuffer);
            return 
    $imgoutput;
          }
        } 
    $img is the input image, $thumbmaxheight and $thumbmaxwidth indicate the maximum size of the resized image. If you know the input images are going to be JPEGs only, then you can strip out the stuff relating to the other formats.

    The imagepng function is set to create the output with a compression of '4' (it ranges from 0 to 9, 0 being no compression = highest quality). You can change that depending on how the images come out.

    Alternatively, you can use the imagejpeg function instead which works in exactly the same way except that the quality goes from 0 to 100 (100 being best). I haven't used imagejpeg but you might find it gives you smaller filesizes for larger images than imagepng does. Not sure though.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    I wrote this script to make thumbnails, which is pretty much the same as what you want to do. It outputs in PNG, so the quality should still be good.
    PHP Code:
    function resize($img$thumbmaxheight$thumbmaxwidth) {
          
    $imgsize getimagesize($img) or die("$img is not a valid image");
          list(
    $width$height) = $imgsize;
          if (
    $thumbmaxwidth $width && $width $height) {
            
    $thumbwidth $thumbmaxwidth;
            
    $thumbheight = ($thumbwidth $width) * $height;
          }
          elseif (
    $thumbmaxheight $height && $height $width) {
            
    $thumbheight $thumbmaxheight;
            
    $thumbwidth = ($thumbheight /$height) * $width;
          }
          else {
            return 
    'No resizing necessary';
          }
          
    $imgbuffer imagecreatetruecolor($thumbwidth$thumbheight);
          if (
    $imgsize['mime'] == "image/jpeg") {
            
    $image imagecreatefromjpeg("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/gif") {
            
    $image imagecreatefromgif("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/png") {
            
    $image imagecreatefrompng("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/bmp") {
            
    $image imagecreatefromwbmp("$album/$img");
          }
          if (!
    $image) {return false;} else {
            
    imagecopyresampled($imgbuffer$image0000$thumbwidth$thumbheight$width$height);
            
    $imgoutput imagepng($imgbuffer"location/for/newimage.png"4);
            
    imagedestroy($imgbuffer);
            return 
    $imgoutput;
          }
        } 
    $img is the input image, $thumbmaxheight and $thumbmaxwidth indicate the maximum size of the resized image. If you know the input images are going to be JPEGs only, then you can strip out the stuff relating to the other formats.

    The imagepng function is set to create the output with a compression of '4' (it ranges from 0 to 9, 0 being no compression = highest quality). You can change that depending on how the images come out.

    Alternatively, you can use the imagejpeg function instead which works in exactly the same way except that the quality goes from 0 to 100 (100 being best). I haven't used imagejpeg but you might find it gives you smaller filesizes for larger images than imagepng does. Not sure though.
    Thanks for posting this. I'm wondering something, on upload, should I use this code to replace move_uploaded_file()? Since I just want to resize the Original image, discard the Original image, and save the new image.
    MattsBlog.Ca
    LiveMP -- Coming soon!

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, what you can do is not bother with move_uploaded_file() and simply run the above script, put the resized image wherever you want using the imagepng() function's second argument and then delete the original:
    PHP Code:
    unlink($img); 

  5. #5
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to change it slightly so you can pass in the destination to save the file to:
    PHP Code:
    function resize($img$thumbmaxheight$thumbmaxwidth$destination) {
          
    $imgsize getimagesize($img) or die("$img is not a valid image");
          list(
    $width$height) = $imgsize;
          if (
    $thumbmaxwidth $width && $width $height) {
            
    $thumbwidth $thumbmaxwidth;
            
    $thumbheight = ($thumbwidth $width) * $height;
          }
          elseif (
    $thumbmaxheight $height && $height $width) {
            
    $thumbheight $thumbmaxheight;
            
    $thumbwidth = ($thumbheight /$height) * $width;
          }
          else {
            return 
    'No resizing necessary';
          }
          
    $imgbuffer imagecreatetruecolor($thumbwidth$thumbheight);
          if (
    $imgsize['mime'] == "image/jpeg") {
            
    $image imagecreatefromjpeg("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/gif") {
            
    $image imagecreatefromgif("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/png") {
            
    $image imagecreatefrompng("$album/$img");
          }
          elseif (
    $imgsize['mime'] == "image/bmp") {
            
    $image imagecreatefromwbmp("$album/$img");
          }
          if (!
    $image) {return false;} else {
            
    imagecopyresampled($imgbuffer$image0000$thumbwidth$thumbheight$width$height);
            
    $imgoutput imagepng($imgbuffer$destination4);
            
    imagedestroy($imgbuffer);
            return 
    $imgoutput;
          }
        } 
    The it's simply a matter of replace move_uploaded_file as you say:
    PHP Code:
    move_uploaded_file($here$there);

    // Becomes...

    resize($here200200$there); 
    Where 200 and 200 are the maximum dimensions (X, Y) of your new thumbnail.

    Edit: And as Raffles points out, you might want to delete the original file afterwards as well.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, thanks for clearing it both of you. I figured that you would just replace move_uploaded_file() with resize().
    MattsBlog.Ca
    LiveMP -- Coming soon!


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
  •