Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    resize image on the fly from dynamic image src

    Hi,

    There are lots of scripts on the web that resize an image, when the url is known e.g.
    http://www.wprecipes.com/how-to-resi...ges-on-the-fly

    However, I need to resize an image from a blob in a table field rather than an existing file.

    Is there a way to do this?
    I'm using Oracle tables

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Same was as from a file.
    Instead of loading via the imagecreatefrom*, you instead use the imagecreatefromstring function. This will give you a resource that can be manipulated via the image* functions from the gd library. Continue as normally from that point.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for this.

    I'm now trying to crop a cut out window of 67(w)x49(h) of an image and echo onscreen.

    Are there any simple examples out there that do this?

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I've found this code that does what I need. But I cant figure out how to display the cropped image on screen?

    I've put the header in but get:
    Code:
    Warning: Cannot modify header information - headers already sent by (output started at D:\Program Files\XAmpp\xampp\htdocs\thumbnail.php:20) in D:\Program Files\XAmpp\xampp\htdocs\thumbnail.php on line 23
    PHP Code:
    <?php

    // Crop dimensions.
    $width 67;
    $height 49;
    // Set the path to the image to resize
    $input_image "images/myimage.jpg";
    // Get the size of the original image into an array
    $size getimagesize$input_image );
    // Prepare canvas
    $canvas imagecreatetruecolor$width$height );
    // Create a new image in the memory from the file 
    $cropped imagecreatefromjpeg$input_image );
    // Prepare image  crop - center the crop on the image
    $newwidth $size[0] / 2;
    $newheight $size[1] / 2;
    $cropLeft = ( $newwidth/) - ( $width/);
    $cropHeight = ( $newheight/) - ( $height/);
    // Generate the cropped image
    echo imagecopyresized$canvas$cropped0,0$cropLeft$cropHeight$size[0], $size[1], $newwidth$newheight );
    // Save the cropped image as cropped.jpg
    //imagejpeg( $canvas, "cropped.jpg" );

    header('Content-type: image/jpeg');
    imagejpeg($canvas);
    // Clear the memory of the tempory images

    imagedestroy$canvas );
    imagedestroy$cropped );
    ?>

  • #5
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    header declarations need to be before any output - you're echoing out the image before sending the headers.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Edited: I'd totally overlooked/missed the first echo in that code.
    Last edited by MattF; 03-20-2010 at 09:31 PM.

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for the replies.
    I've found the problem, there was an extra echo in there I hadn't spotted. Removed that and it works fine...

    PHP Code:
    <?php
    // Crop dimensions.
    $width 67;
    $height 49;
    // Set the path to the image to resize
    $input_image "images/news/news.jpg";
    // Get the size of the original image into an array
    $size getimagesize$input_image );
    // Prepare canvas
    $canvas imagecreatetruecolor$width$height );
    // Create a new image in the memory from the file 
    $cropped imagecreatefromjpeg$input_image );
    // Prepare image  crop - center the crop on the image
    $newwidth $size[0] / 2;
    $newheight $size[1] / 2;
    $cropLeft = ( $newwidth/) - ( $width/);
    $cropHeight = ( $newheight/) - ( $height/);
    // Generate the cropped image
    imagecopyresized$canvas$cropped0,0$cropLeft$cropHeight$size[0], $size[1], $newwidth$newheight );

    header('Content-type: image/jpeg');
    imagejpeg($canvas);
    // Clear the memory of the tempory images

    imagedestroy$canvas );
    imagedestroy$cropped );
    ?>
    However, the cropped window isn't from the center of the image, but instead the top left corner. How can I edit this to do the crop from the center?

  • #8
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Its okay - I've found some even better code:
    PHP Code:
    <?php
    function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
        //getting the image dimensions 
        
    list($width_orig$height_orig) = getimagesize($imgSrc);  
        
    $myImage imagecreatefromjpeg($imgSrc);
        
    $ratio_orig $width_orig/$height_orig;
       
        if (
    $thumbnail_width/$thumbnail_height $ratio_orig) {
           
    $new_height $thumbnail_width/$ratio_orig;
           
    $new_width $thumbnail_width;
        } else {
           
    $new_width $thumbnail_height*$ratio_orig;
           
    $new_height $thumbnail_height;
        }
       
        
    $x_mid $new_width/2;  //horizontal middle
        
    $y_mid $new_height/2//vertical middle
       
        
    $process imagecreatetruecolor(round($new_width), round($new_height));
       
        
    imagecopyresampled($process$myImage0000$new_width$new_height$width_orig$height_orig);
        
    $thumb imagecreatetruecolor($thumbnail_width$thumbnail_height);
        
    imagecopyresampled($thumb$process00, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width$thumbnail_height$thumbnail_width$thumbnail_height);

        
    imagedestroy($process);
        
    imagedestroy($myImage);
        return 
    $thumb;
    }

    //Create the thumbnail
    $newThumb CroppedThumbnail("images/news/portrait.jpg",67,49);

    // And display the image...
    header('Content-type: image/jpeg');
    imagejpeg($newThumb);
    ?>
    Taken from seifer at loveletslive dot com's post at http://php.net/manual/en/function.im...yresampled.php


  •  

    Posting Permissions

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