PHP Code:
<?PHP
include("config.inc.php");
# Constants
define(MAX_WIDTH, 450);
define(MAX_HEIGHT, 450);
# Get image location
$image_file = "11.jpg";//str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = "$images_dir" . "/$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(200, 200);
imagecolorallocate($img, 255, 255, 255); //white image
$black = imagecolorallocate($img, 0, 0, 0);
$red = imagecolorallocate($img, 255, 0, 0);
imageline($img, 0, 0, 199, 0, $black); //top border
imageline($img, 0, 0, 0, 199, $black); //left border
imageline($img, 199, 0, 199, 199, $black); //right border
imageline($img, 0, 199, 199, 199, $black); //bottom border
imageline($img, 0, 0, 199, 199, $red); //cross hatch from top left to bottom right
imageline($img, 199, 0, 0, 199, $red); //cross hatch from bottom left to top right
$text_color = ImageColorAllocate($img, 0, 0, 0);
ImageString($img, 3, 66, 10, "Not Found!", $text_color); //text on image
}
# Display the image
header("Content-type: image/png");
imagepng($img);
?>
I know its not exactly what we are looking for yet but it could be a start. Its also pretty slow because it uses the imagecopyresampled() function to better smooth the image. This function and script for that matter will only work with GD v. 2+, sorry pre-2 people...
Bookmarks