/ Published in: PHP
Not particularly novel, but quite clean. Allows you to put images in any location on the server and saves having to generate thumbnails at upload time.
The URL to this file should be:
`//`
Valid request types:
* `view` — View the image full-size (don't scale)
* `preview` — View the image at a medium scale
* `thumb` — Show a thumbnail of the image
Images can be JPEGs or PNGs. Broken URLs redirect to a designated 404 image.
Thumbnails and previews will be stored using an identical folder hierarchy inside the `thumbnails` and `previews` folders inside the images folder.
The URL to this file should be:
`//`
Valid request types:
* `view` — View the image full-size (don't scale)
* `preview` — View the image at a medium scale
* `thumb` — Show a thumbnail of the image
Images can be JPEGs or PNGs. Broken URLs redirect to a designated 404 image.
Thumbnails and previews will be stored using an identical folder hierarchy inside the `thumbnails` and `previews` folders inside the images folder.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php $imagesFolder = 'images/'; // Change this if required to point to your source folder $brokenImage = $imagesFolder.'brokenimage.png'; $thumbnailHeight = $thumbnailWidth = 80; // Set to the max size of your thumbnail $previewHeight = $previewWidth = 250; // Set to the max size of your preview images date_default_timezone_set('Australia/NSW'); // Remove or update this line depending on your PHP.ini settings try { throw new exception('Invalid path'); } if($pathComponents[0] === 'thumb') { $outputWidth = $thumbnailWidth; $outputHeight = $thumbnailHeight; } else { $outputWidth = $previewWidth; $outputHeight = $previewHeight; } function mkdir_recursive($pathname, $mode = 0777) { } throw new exception('Invalid path'); } // Generate a smaller image if we haven't already got one $thumbFile = str_replace($imagesFolder, $imagesFolder.($pathComponents[0] === 'thumb' ? 'thumbnails' : 'previews').'/', $file); // Generate a thumbnail switch($fileExtension) { case 'jpg': case 'jpeg': break; case 'png': break; } if ($sourceX > $sourceY) { $destinationWidth = $outputWidth; $destinationHeight = $sourceY * ($outputHeight/$sourceX); } else if ($sourceX < $sourceY) { $destinationWidth = $sourceX * ($outputWidth/$sourceY); $destinationHeight = $outputHeight; } else if ($sourceX == $sourceY) { $destinationWidth = $outputWidth; $destinationHeight = $outputHeight; } imagecopyresampled($outputImage, $sourceImage, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceX, $sourceY); // Make sure the destination folder path exists switch($fileExtension) { case 'jpg': case 'jpeg': break; case 'png': break; } } $file = $thumbFile; } outputImage($brokenImage); } outputImage($file); } catch(exception $ex) { outputImage($brokenImage); } function outputImage($file) { switch($fileExtension) { case 'jpg': case 'jpeg': break; case 'png': break; } exit; } function showBrokenImage($errno, $errstr) { global $brokenImage; outputImage($brokenImage); } return true; // Don't execute PHP internal error handler } ?>