Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below is a thumbnailer I wrote in PHP. I've stripped out the bits that add drop shadows and borders (Don't think i've broken it but haven't tested). This uses the GD library in PHP and I've always been happy with the results.</p> <p>NB: You can probably strip out more - eg it sets the BG colour of the thumbnail so that it matches the page background, etc...</p> <p>In this case, it would be called like this:</p> <pre><code>thumbnail.php?size=400&amp;image=SomeImage.jpg </code></pre> <p>The only slight issue is that with large files (ie very high quality from modern digital cameras) it can have memory issues. I rarely hit this problem though - usually anything that size can't be uploaded by a user as the webserver won't allow it.</p> <pre><code>&lt;?php $defaultsize = 400; $defaultimage = "images/error.jpg"; ini_set("memory_limit", "32M"); $red = isset($_REQUEST['r']) ? $_REQUEST['r'] : 255; $green = isset($_REQUEST['g']) ? $_REQUEST['g'] : 255; $blue = isset($_REQUEST['b']) ? $_REQUEST['b'] : 255; if(!isset($_REQUEST['size'])) { $maxWidth=$defaultsize; $maxHeight=$defaultsize; } else { $maxWidth=$_REQUEST['size']; $maxHeight=$_REQUEST['size']; } if(!isset($_REQUEST['image'])) { $picurl = $defaultimage; } else { $picurl = "../" . stripslashes($_REQUEST['image']); } //Find out about source file $srcDetails = @getimagesize($picurl); if($srcDetails) { $srcWidth=$srcDetails[0]; $srcHeight=$srcDetails[1]; } else { $srcWidth=$maxWidth; $srcHeight=$maxHeight; } if($srcWidth/$srcHeight &lt; $maxWidth/$maxHeight) { //Too wide $width = $maxHeight / $srcHeight * $srcWidth; $height = $maxHeight / $srcHeight * $srcHeight; } else { //Too tall $width = $maxWidth / $srcWidth * $srcWidth; $height = $maxWidth / $srcWidth * $srcHeight; } switch ($srcDetails[2]) { case 1: //GIF $srcImage = ImagecreateFromGIF($picurl); break; case 2: //JPEG $srcImage = ImagecreateFromJPEG($picurl); break; case 3: //PNG $srcImage = ImagecreateFromPNG($picurl); break; case 6: //WBMP $srcImage = ImagecreateFromWBMP($picurl); break; default: //Possibly add some "Unknown File Type" error code here. However, if we do't return an image, we will error nicely later anyway break; } if(@!$srcImage) { // The nice error for no source image (include error mail to yourself here if you want...) $srcImage = imagecreate($maxWidth, $maxHeight); /* Create a blank image */ $bgc = imagecolorallocate($srcImage, 255, 255, 255); $tc = imagecolorallocate($srcImage, 0, 0, 0); imagefilledrectangle($srcImage, 0, 0, 150, 30, $bgc); /* Output an errmsg */ imagestring($srcImage, 4, 5, 5, "Error resizing image", $tc); imagestring($srcImage, 4, 5, 20, "Tech support department", $tc); imagestring($srcImage, 4, 5, 35, "has been informed", $tc); } //Create thumbnail $thumb = imagecreatetruecolor ($width, $height); $bg = ImageColorAllocate($thumb, $red, $green, $blue); imagefill ($thumb, 0, 0, $bg); //Add the image itself Imagecopyresized ($thumb, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); //Add a black border imageline($thumb, 0, 0, 0, $height, $black); imageline($thumb, 0, 0, $width, 0, $black); imageline($thumb, 0, $height, $width, $height, $black); imageline($thumb, $width, $height, $width, 0, $black); //output header //I leave this so late so if there ARE any errors, they are displayed as text not a broken image //(this will happen when looking at the thumnailer directly but will display as a broken image in a webpage still) header("Content-type: image/PNG"); imagePNG($thumb); //Clear up memory imagedestroy($srcImage); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload