php缩略显示数据,生成缩略图保存数据库_PHP教程

该代码段展示了一个PHP脚本,用于从URL获取图像,根据预设的最大宽度和高度调整其尺寸,同时保持原始比例。如果图片超过最大尺寸,它将被缩放。若加载失败,脚本会创建一个错误图像。最后,图像以JPEG格式显示并销毁。
摘要由CSDN通过智能技术生成

# Constants

define("IMAGE_BASE", './');

define("MAX_WIDTH", 150);

define("MAX_HEIGHT", 150);

# Get image locationstr_replace('..', '', $_SERVER['QUERY_STRING']);

$image_file = 't.jpg';

$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 =150; //floor($scale*$width);

$new_height =150;// 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);

imagedestroy($img);

?>

http://www.bkjia.com/PHPjc/445089.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445089.htmlTechArticle?php # Constants define(IMAGE_BASE, './'); define(MAX_WIDTH, 150); define(MAX_HEIGHT, 150); # Get image locationstr_replace('..', '', $_SERVER['QUERY_STRING']); $image_file = 't.jp...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值