PHP生成缩略图的实现

王朝php·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

PHP令我们惊喜的就是在图形图象处理方面要忧于ASP,用GD库PHP就可以轻松的实现缩略图。这一篇文章我们的目的就是用GD来生成缩略图,PHP可以把缩略图直接生成输送到浏览器也可以以文件的形式把其存储到硬盘当中。

在生成缩略图的过程当中我们需要用到GD库当中的几个函数:

getimagesize(string filename [,array var])),取得图像的信息,返回值是一人array,包括几项信息$var[0]----返回图像的width,$var[1]----返回height,[2]返回图像文件的type,[4]返回的是与<img src="">当中的wdith,height有关的width="",height=""信息。

imageX(resource image)

imageY(resource image) 返回图像的宽和高

imagecopyresized(des img,src img,int des_x,int des_y,int src_x,int src_y,int des_w,int des_h,int src_w,int src_y) 复制并截取区域图像

imagecreatetruecolor(int width,int height) 创建一个真彩图

imagejpeg(resource image)

下面就是Code:

<?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,

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航