1

I have found and modified a small php script for generating thumbnails

$src = (isset($_GET['file']) ? $_GET['file'] : "");
$width = (isset($_GET['maxwidth']) ? $_GET['maxwidth'] : 73);
$thname = "xxx";

$file_extension = substr($src, strrpos($src, '.')+1);

switch(strtolower($file_extension)) {
     case "gif": $content_type="image/gif"; break;
     case "png": $content_type="image/png"; break;
     case "bmp": $content_type="image/bmp"; break;
     case "jpeg":
     case "jpg": $content_type="image/jpg"; break;

     default: $content_type="image/png"; break;

}

if (list($width_orig, $height_orig, $type, $attr) = @getimagesize($src)) {
    $height = ($width / $width_orig) * $height_orig;
}

$tn = imagecreatetruecolor($width, $height) ;
$image = imagecreatefromjpeg($src) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($tn, './media/'.$thname.'.'.$file_extension, 90);

It generates and saves thumbnails perfectly.

How can I display those thumbnails on the fly?

I tryed to add this at the bottom of a script

header('Content-Type: image/jpeg');
imagegd($image);

but it says The image cannot be displayed because it contains errors. What am I doing wrong?

4
  • 3
    Look in the image's source code using a text editor; you will likely have a PHP error message in there. Commented Jul 31, 2012 at 11:47
  • As I said: "It generates and saves thumbnails perfectly" Commented Jul 31, 2012 at 11:49
  • Ah, so you added that code to a HTML page. That won't work; you will need to embed each result in a <img> tag. You could show them on the fly using DATA URI's, but that won't work well in Internet Explorer, and not at all in older versions Commented Jul 31, 2012 at 11:55
  • Whitespace at the top of the script was the problem. Thank you very much for your effort Commented Jul 31, 2012 at 12:18

3 Answers 3

4

In php the simplest method is using imagejpeg() function.

In one of my solution I have created Image thumbnails using this function in which I can specify the height and width.

Below is the code snippet for the same:

<?php
/*www.ashishrevar.com*/
/*Function to create thumbnails*/
function make_thumb($src, $dest, $desired_width) {
  /* read the source image */
  $source_image = imagecreatefromjpeg($src);
  $width = imagesx($source_image);
  $height = imagesy($source_image);

  /* find the “desired height” of this thumbnail, relative to the desired width  */
  $desired_height = floor($height * ($desired_width / $width));

  /* create a new, “virtual” image */
  $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

  /* copy source image at a resized size */
  imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

  /* create the physical thumbnail image to its destination */
  imagejpeg($virtual_image, $dest);
}
make_thumb($src, $dest, $desired_width);
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Try taking the closing ?> off at the end of the file and make sure that there is no whitespace at the top of the file. All it takes is on newline and the image will break.

1 Comment

I can't beleive it! There was a whitespace at the top of the script. I feel ashamed now :)
2

http://php.net/manual/en/function.imagegd.php

header('Content-Type: image/jpeg');
imagegd($image);

2 Comments

still the same error; The image cannot be displayed because it contains errors
@Goldie then as said, look into the image's source code to see what's wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.