福建省农业农村经济发展态势良好 农产品价格将继续高位运行 不会引发全面通胀
Google
      
打印

PHP生成缩略图文件

本主题由 四海权威 于 2008-6-28 17:46 置顶

PHP生成缩略图文件

转载
Copy code

<?
$FILENAME="image_name";
// 生成图片的宽度
$RESIZEWIDTH=100;
// 生成图片的高度
$RESIZEHEIGHT=100;
//生成图片的路径
$uploaddir="./";

function ResizeImage($im,$maxwidth,$maxheight,$name){
global $uploaddir;
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$uploaddir.$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$uploaddir.$name . ".jpg");
}
}

if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
?>

<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>

<form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="浏览"><p>
<input type="submit" value="上传图片">
</form>

</body>
</html>

TOP

转载2
Copy code客户端上载的页面:
-------------------- upload.htm ---------------------
<form enctype="multipart/form-data"
method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000000">请选择或输入上传文件名:<input name="upfile" type="file">
<input type="submit" value="开始上传">


--------------------- upload.php -------------------
<?
if(empty($upfile)){
print("No file was transfered!\n无文件上传!");
exit;
}
$image_path=$upfile;
@$image_info=getimagesize($image_path);
if($image_info[2]==2)
{@$image2=imagecreatefromjpeg($image_path);
}
else if($image_info[2]==1){
@$image2=imagecreatefromgif($image_path);
}
else{print("不接受的图片!");exit;}
if(empty($image2)){print("系统错误,请重试");exit;}
$image2_x=imagesx($image2);
$image2_y=imagesy($image2);
if($image2_x==$image2_y){
$x=150;
$y=0;
}
else if($image2_x>$image2_y){
$x=150;
$y=intval(150*$image2_y/$image2_x);
}
else{
$y=150;
$x=intval(150*$image2_x/$image2_y);
}
$image1=imagecreate($x,$y);

imagecopyresized($image1,$image2,0,0,0,0,$x,$y,$image2_x,$image2_y);
imagegif($image1,"new.gif");
?>

------------------------- 说明 ----------------------
本代码特点在于将所有难看的warning转换为可以自已设计输出的出错提示,@作用在于抑制出借信息。此代码默认的是将图片转换为长宽不超过150像素的缩略图,并保存为new.gif 。使用时可以随意修改。

TOP

转载3
Copy code生成缩略图  
$tx=GetImageSize( $sample);
if( $tx[0]<= $tx[1] and  $tx[1]>=120){
 $height=120;
 $width=intval( $height* $tx[0]/ $tx[1]);
}
if( $tx[0]>= $tx[1] and  $tx[0]>=100){
 $width=100;
 $height=intval( $width* $tx[1]/ $tx[0]);
}
if( $tx[0]<100 and  $tx[1]<120){
 $width= $tx[0];
 $height= $tx[1];
}

makethumb2( $sample, $target, $width, $height);

//  $srcFile: 源文件
//  $dstFile: 目标文件
//  $dstW: 目标图片宽度
//  $dstH: 目标文件高度
function makethumb2( $srcFile, $dstFile, $dstW, $dstH){
 $data=GetImageSize( $srcFile,& $info);
switch( $data[2]){
case 1:
 $im=@ImageCreateFromGIF( $srcFile);
break;
case 2:
 $im=@ImageCreateFromJPEG( $srcFile);
break;
case 3:
 $im=@ImageCreateFromPNG( $srcFile);
break;
}
 $srcW=ImageSX( $im);
 $srcH=ImageSY( $im);
 $ni=ImageCreate( $dstW, $dstH);
ImageCopyResized( $ni, $im,0,0,0,0, $dstW, $dstH, $srcW, $srcH);
ImageJpeg( $ni, $dstFile);
// 如果需要输出到浏览器,那么将上一句改为ImageJpeg( $ni);
// 如果需要其它格式的图片,改动最后一句就可以了
}

TOP

PHP批量生成缩略图
<转载>
本文件可以用于自动创建目录下所有JPG图片的缩略图,放到图片目录下运行即可。
缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。



Copy code<?php
$config = array();
$config['path'] = "./";
$config['t_width'] = 120;
$config['t_height'] = 98;
$config['ignore'] = array("",".","..");
$config['prefix'] = "thumb_";
$done = 0;
define("IMAGE_JPG", 2);
define("ENDL", "\n");
if($handle = opendir($config['path'])) {
    while(false !== ($file = readdir($handle))) {
        if(!array_search($file,$config['ignore'])) {
            
            list($im_width, $im_height, $type) = getimagesize($file);
            if($type != IMAGE_JPG) {
                continue;
            }
            
            $op .= "found -> <a href='{$file}'>$file</a>" . ENDL;
            $im = @imagecreatefromjpeg($file);
            if(!$im) {
                $op .= "fail  -> couldn't create sour image pointer." . ENDL;   
                continue;
            }
            
            if(file_exists($config['prefix'] . $file) || substr($file, 0, strlen($config['prefix'])) == $config['prefix']) {
                $op .= "note  -> this file has already got a thumbnail." . ENDL;   
                continue;
            }
            $to = imagecreatetruecolor($config['t_width'],$config['t_height']);
            if(!$to) {
                $op .= "fail  -> couldn't create dest image pointer." . ENDL;   
                continue;
            }            
            
            if(!imagecopyresampled($to, $im, 0, 0, 0, 0, $config['t_width'], $config['t_height'], $im_width, $im_height)) {
                $op .= "fail  -> couldn't create thumbnail. php fail." . ENDL;   
                continue;
            }
            
            //保存文件
            imagejpeg($to, $config['prefix'] . $file);
            $op .= "done  -> created thumb: <a href='{$config['prefix']}{$file}'>{$config['prefix']}{$file}</a>" . ENDL;   
            $done++;
        }
    }
}
closedir($handle);
$op .= "fin  -> {$done} file(s) written" . ENDL;
echo "<pre>";
echo $op;
echo "</pre>";
exit;
?>

TOP

Copy code<?php
//image test
header("Content-type: image/jpeg");
$picfile='2.jpg';
$im=imagecreatefromjpeg($picfile);
$color=imagecolorallocate($im,255,255,255);
imagestring($im,1,5,5,"imbeded string",$color);
$filesize=getimagesize($picfile);

$output = imagecreatetruecolor(100,100);
if ( !$output ) {
error("Generate Error!");
return false;
}
imagecopyresampled($output,$im,0,0,0,0,100,100,$filesize[0],$filesize[1]);
$new='img/'.uniqid('s').$picfile;

TOP

Copy codePHP生成图片缩略图2007-01-21 23:33要使用PHP生成图片缩略图,要保证你的PHP服务器安装了GD2图形库
使用一个类生成图片的缩略图,类的源码见下文


调用此类的方法:
$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");
//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高


//使用如下类就可以生成图片缩略图,  PHP代码如下:
<?php
class resizeimage
{
    //图片类型
    var $type;
    //实际宽度
    var $width;
    //实际高度
    var $height;
    //改变后的宽度
    var $resize_width;
    //改变后的高度
    var $resize_height;
    //是否裁图
    var $cut;
    //源图象
    var $srcimg;
    //目标图象地址
    var $dstimg;
    //临时创建的图象
    var $im;

    function resizeimage($img, $wid, $hei,$c,$dstpath)
    {
        $this->srcimg = $img;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;
        //图片的类型
   
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));

        //初始化图象
        $this->initi_img();
        //目标图象地址
        $this -> dst_img($dstpath);
        //--
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
        //生成图象
        $this->newimg();
        ImageDestroy ($this->im);
    }
    function newimg()
    {
        //改变后的图象的比例
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        //实际图象的比例
        $ratio = ($this->width)/($this->height);
        if(($this->cut)=="1")
        //裁图
        {
            if($ratio>=$resize_ratio)
            //高度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            //宽度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
        else
        //不裁图
        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
    }
    //初始化图象
    function initi_img()
    {
        if($this->type=="jpg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
    //图象目标地址
    function dst_img($dstpath)
    {
        $full_length  = strlen($this->srcimg);

        $type_length  = strlen($this->type);
        $name_length  = $full_length-$type_length;


        $name        = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $dstpath;


//echo $this->dstimg;
    }
}
?>  





/////////////////////////////////////////////////////////////////////
php imagecopyresampled使用方法


使用函式:

bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
函式参数说明:

dst_image : 输出目標档案
src_image : 来源档案
dst_x: 目標档案开始点的 x 座標
dst_y: 目標档案开始点的 y 座標
src_x: 来源档案开始点的 x 座標
src_y: 来源档案开始点的 y 座標
dst_w: 目標档案的长度
dst_h: 目標档案的高度
src_w: 来源档案的长度

TOP