#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2

    Thumbnail function error


    I keep getting the error "the image cannot be displayed because it contains errors" every time I try to use header('Content-Type: image/jpeg'). The function looks right to me so i'm not sure what i'm missing. I need to output the image to the browser without saving it.

    PHP Code:
    function thumbnail($path){
        
    $info getimagesize($path);
        
    $size = array($info[0], $info[1]);
        
        if(
    $size[0] > 200){
            
    $width 200;
        }else{
            
    $width $size[0];
        }
        
        if(
    $size[1] > 200){
            
    $height 200;
        }else{
            
    $height $size[1];
        }
        
        if (
    $info['mime'] == 'image/png'){
            
    $src imagecreatefrompng($path);
        }elseif (
    $info['mime'] == 'image/jpeg'){
            
    $src imagecreatefromjpeg($path);
        }elseif (
    $info['mime'] == 'image/gif'){
            
    $src imagecreatefromgif($path);
        }else{
            return 
    false;
        }
        
        
    $thumb imagecreatetruecolor($width$height);
        
        
    $src_aspect $size[0] / $size[1];
        
    $thumb_aspect $width $height;
        
        if(
    $src_aspect<$thumb_aspect){
            
    //narrower
            
    $scale $width $size[0];
            
    $new_size = array($width$width/$src_aspect);
            
    $src_pos = array(0, ($size[1] * $scale $height) / $scale 2);
        }elseif(
    $src_aspect>$thumb_aspect){
            
    //wider
            
    $scale $height $size[1];
            
    $new_size = array($height $src_aspect$height);
            
    $src_pos = array(($size[0] * $scale $width) / $scale 2,0);
        }else{
            
    //same shape
            
    $new_size = array($width$height);
            
    $src_pos = array(0,0);
        }
        
        
    $new_size[0] = max($new_size[0], 1);
        
    $new_size[0] = max($new_size[1], 1);
        
        
    imagecopyresampled($thumb$src0,0$src_pos[0],$src_pos[1], $new_size[0],$new_size[1], $size[0], $size[1]);
        
        
    header('Content-Type: image/jpeg');
        return 
    imagejpeg($thumb);
        
        
    imagedestroy($thumb);

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,350
    Rep Power
    594
    That is not a PHP error so I am guessing you see this on your browser not on the server. It looks to me like it is not a PHP problem. It may be the image is indeed corrupt. Did you try a different browser or OS?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Originally Posted by gw1500se
    That is not a PHP error so I am guessing you see this on your browser not on the server. It looks to me like it is not a PHP problem. It may be the image is indeed corrupt. Did you try a different browser or OS?
    Must be corrupt file because i'm just seeing image output code in IE. All the images have been previously recreated using a watermark.

    This whole thumbnail process has been more trouble than it's worth, I think i'll just save a copy of the thumbnail and create a function to delete them later.

IMN logo majestic logo threadwatch logo seochat tools logo