Jump to content

[SOLVED] Thumbnail problem


Yesideez

Recommended Posts

Is it possible to have a function inside a script to generate a thumbnail of an image and display it in the browser without having to use an external script?

 

I've got a script that shows the status of the background image, here's the line of code that is trying to display the image:

  <tr><td>Image</td><td><a href="<?php echo '../uploads/images/'.$image->name; ?>" rel="lightbox" title="Background Image" <?php echo htmlentities($image->name); ?>><?php imageResize($basedir.'/../uploads/images/'.$image->name,$image->type,false,null,50); ?></a></td></tr>

 

This is the function imageResize():

function imageResize($source,$type,$target=false,$newWidth=null,$newHeight=null) {
  $result['success']=false;
  switch ($type) {
    case 'jpg':header("Content-type: image/jpg");$src=@imagecreatefromjpeg($source);break;
    case 'png':header("Content-type: image/png");$src=@imagecreatefrompng($source);break;
    case 'gif':header("Content-type: image/gif");$src=@imagecreatefromgif($source);break;
  }
  if (isset($src)) { //DID WE CREATE OUR RESOURCE?
    list($imgW,$imgH)=getimagesize($source);
    if ($newWidth==null && $newHeight==null) { //WIDTH & HEIGHT NOT SPECIFIED SO DON'T RESIZE
      $newWidth=$imgW;
      $newHeight=$imgH;
    } else if ($newHeight==null) { //CALCULATE NEW HEIGHT CONSTRAINING THE DIMENSIONS
      $newHeight=intval(($imgH/$imgW)*$newWidth);
    } else { //CALCULATE NEW WIDTH CONSTRAINING THE DIMENSIONS
      $newWidth=intval(($imgW/$imgH)*$newHeight);
    }
    $tmp=@imagecreatetruecolor($newWidth,$newHeight);
    if ($tmp) { //DID WE CREATE OUR IMAGE?
      if (@imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$imgW,$imgH)) { //DID WE RESIZE THE IMAGE?
        if ($target==false) {
          switch ($type) {
            case 'jpg':$img=@imagejpeg($tmp);break;
            case 'png':$img=@imagepng($tmp);break;
            case 'gif':$img=@imagegif($tmp);break;
          }
        } else {
          switch ($type) {
            case 'jpg':$img=@imagejpeg($tmp,$target,100);break;
            case 'png':$img=@imagepng($tmp,$target,0);break;
            case 'gif':$img=@imagegif($tmp,$target);break;
          }
        }
        if ($img) { //DID WE SAVE OUR NEW IMAGE?
          $result['width']=$newWidth;
          $result['height']=$newHeight;
          $result['name']=$target;
          @imagedestroy($src);
          @imagedestroy($tmp);
          $result['success']=true;
        }
      }
    }
  }
  if ($target!=false) {
    return $result;
  }
}

 

If I omit the header() statements the script works but instead of the image being shown I get the raw content of the file - as expected. How can I send the header() info so it displays?

 

I've searched everywhere for examples and all I can find are examples that work in their own files - unless I'm reading something wrong somewhere.

Link to comment
Share on other sites

I've searched everywhere for examples and all I can find are examples that work in their own files - unless I'm reading something wrong somewhere.
That's because that is the way it works. Browsers fetch media content on a page separately through the URL of that media content and for a image you need an <img src="URL_that_resutls_in_the_image_being_output_with_header" alt=""> tag in the link you are forming on the page.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.