SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Resizing images on the fly want to make sure I have it working right

    Alright I have some PHP code which does this:

    PHP Code:
    <?php

    # Constants
    define(IMAGE_BASE'/home/psdesig1/public_html/PHP/');
    define(MAX_WIDTH50);
    define(MAX_HEIGHT50);

    # Get image location
    $image_file str_replace('..'''$_SERVER['QUERY_STRING']);
    //$image_file = 'uploadsCreek.jpg';
    $image_path IMAGE_BASE "/$image_file";

    # Load image
    $img null;
    $ext strtolower(end(explode('.'$image_path)));
    if (
    $ext == 'jpg' || $ext == 'jpeg') {
        
    $img = @imagecreatefromjpeg($image_path);
    } else if (
    $ext == 'png') {
        
    $img = @imagecreatefrompng($image_path);
    # Only if your version of GD includes GIF support
    }

    # If an image was successfully loaded, test the image for size
    if ($img) {

        
    # Get image size and scale ratio
        
    $width imagesx($img);
        
    $height imagesy($img);
        
    $scale min(MAX_WIDTH/$widthMAX_HEIGHT/$height);

        
    # If the image is larger than the max shrink it
        
    if ($scale 1) {
            
    $new_width floor($scale*$width);
            
    $new_height floor($scale*$height);

        
    # Create a new temporary image
        
    $new_width=50;
        
    $new_height=50;
            
    $tmp_img imagecreatetruecolor($new_width$new_height);

            
    # Copy and resize old image into new image
            
    imagecopyresized($tmp_img$img0000
                             
    $new_width$new_height$width$height);
            
    imagedestroy($img);
            
    $img $tmp_img;        
        }    
    }

    # Create error image if necessary
    if (!$img) {
        
    $img imagecreate(MAX_WIDTHMAX_HEIGHT);
        
    imagecolorallocate($img,0,0,0);
        
    $c imagecolorallocate($img,70,70,70);
        
    imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
        
    imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
    }

    # Display the image
    header("Content-type: image/jpeg");
    imagejpeg($img);


    ?>
    So as far as I can understand it resizes an image and returns the resized image. So with my sample HTML file I did this:
    HTML Code:
    <img src="http://psdesignzone.co/PHP/thumbnailwv4.php?uploadsCreek.jpg">
    You can see an example of it running here:

    http://psdesignzone.com/PHP/TestDisplay.html

    I think it's working but my main question is it seems to me that the way i'm doing it, it is resizing the image each time you load the TestDisplay.html page. Is it doing that? If so is there anyway to detect if the image has already been resized and then not resize it ever again?

    Thanks for any tips, looking for the best way to dynamically resize images on the Fly in PHP

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,443
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    I do not see how you can detect if the image has been resized unless you save it; you could then use getimagesize() to check the size first.

    Once the browser moves on the image will be lost as it only exists in the browser ? As you say everytime the page is loaded it resizes the image.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since this resizing doesn't appear to need to be dynamic, why not just resize them images once, and then let the webserver serve the files? It would be much more efficient.

    You could just resize them upon upload, and then save to the proper directory. Or, is resizing upon upload not really an attractive option for you?

  4. #4
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Since this resizing doesn't appear to need to be dynamic, why not just resize them images once, and then let the webserver serve the files? It would be much more efficient.

    You could just resize them upon upload, and then save to the proper directory. Or, is resizing upon upload not really an attractive option for you?
    I would agree that would be the best way, unfortunately I already have about 20 or so "unsanitized images" I'd like an easy way to display these images at 50x50 pixels without having to download them resize them and then upload them.

    example PseudoCode

    if(image right size){
    display image
    }
    if(image not right size){
    resizeImage(image);
    display image
    }


    To get a better idea of what I need look here:

    http://psdesignzone.com/PHP/hotListing.php

    That displays my three top tutorials, I need to properly resize and format the images so that they will fit underneath the "hot" listing on this page:

    http://psdesignzone.com/PHP/uploads/v5/layout.html

    As you can see they need to be 50x50 pixels. And the ones I currently have are various sizes.

    Anyone know of a good way of doing that? I'm having a hard time wrapping my brain around the automatic resizing, it's pretty new to me.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would just make a new directory, and only keep properly sized images in it.
    You could have another directory which holds the origionals.

    You can save an image to the filesystem by supplying a filename to imagejpeg()

    You could:
    -use glob() to loop through all 20 existing images, and resize them and save them.
    -keep it as a runtime script, but have the script do a file_exists() in the "already resized" directory. If it doesn't exist, it looks in the other dir, resizes, and saves.

    I recomend you do the first one, and just resize the images when uploading. You can use php to resize when uploading. The benefit here is you get to use your webserver to output the files then, which is more efficient, and automatically sends appropriate http headers so browsers can better cache the images.

  6. #6
    SitePoint Evangelist JordashTalon's Avatar
    Join Date
    Dec 2008
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I'm going to try your second method, my reason is I may need to have multiple image sizes (50x50, 20x20 etc..) and I was thinking of creating directories named 50x50thumbs and scan through that directory and display image but if image doesn't exist look through bigger directory then resize image.


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
  •