SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems clipping & displaying an image

    I am trying to extract a clip of a particular size from an image and display that clip on a web page using php. I have some code that will do that, however, I don't know how to use it within the context of an actual page as the only method I have found for displaying the image includes the use of the header() function. Since I am inserting the images in the body of the page, other content is sent before the header() function called in the clip_image() function and the image in is not displayed.

    My code is below. The commented out echo prior to calling the function prevents the display of the image if I uncomment it. Since I will be inserting a number of clipped images onto the page, there will, of necessity be content sent prior to calling the function that includes the header() function. If I do away with the header() function, I just get a bunch of BLOB data displayed.

    I'm sure there is something obvious here, but I am not seeing it.

    Thanks,

    --Kenoli


    Code:
    <?php
    
    function clip_image($filename) {
    
    // Get width & height of image
    list($current_width, $current_height) = getimagesize($filename);
    
    // Set final crop size
    $crop_width = 150;  $crop_height = 150;
    
    // Calculate origin of crop
    $origin_left = ($current_width - $crop_width)/2;
    $origin_top = ($current_height - $crop_height)/2;
    
    // Create the crop template image and extract the image into a variable
    $src = imagecreatefromjpeg($filename);
    $dest = imagecreatetruecolor(150, 150);
    
    // Clip the image starting at origin given
    imagecopy($dest, $src, 0, 0, $origin_left, $origin_top, $current_width, $current_height);
    
    // Output image
    header('Content-Type: image/jpeg');
    $dest = imagejpeg($dest, $stuff);
    
    // What else can I do here to output the image???
    
    // Clear memory
    imagedestroy($dest);
    imagedestroy($src);
    
    }
    
    //echo 'This is some content sent before the header function';  // This line if uncommented prevents the display of the image
    
    $filename = 'images/arrow.jpg';
    
    $stuff = clip_image($filename);
    
    echo "<img src = \"$stuff\" />";
    
    ?>

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,414
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You need to have the clip code on a separate page like clip.php and insert it in your page with the standard image tags e.g.
    Code:
    <img src="clip.php">
    From memory you can add a variable to the image tag so you can use the same page for different images but it is a long time ago since I used this method!

    It is funny as sometime I needed the header function due to the problem you are seeing and other time the header function caused the problem; I never knew why that was.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I was thinking that this might be the case, i.e. it works if called from a different file. The header() function is odd. I often get confused by it.

    --Kenoli


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
  •