<?php
# constants
define(image_base, '/var/www/html/mbailey/images');
define(max_width, 150);
define(max_height, 150);
# get image location
$image_file = str_replace('..', '', $_server['query_string']);
$image_path = image_base . "/$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
imagecopyresized($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(max_width, max_height);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,max_width,max_height,$c2);
imageline($img,max_width,0,0,max_height,$c2);
}
# display the image
header("content-type: image/jpeg");
imagejpeg($img);
?>
新闻热点
疑难解答