PHP生成缩略图怎么写啊? 把上传好的图片,读取出来的时候是大小一样的缩略图显示,要怎么写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php/*构造函数-生成缩略图+水印,参数说明:$srcFile-图片文件名,$dstFile-另存文件名,$words-水印文字,$image-水印图片,$dstW-图片保存宽度,$dstH-图片保存高度,$rate-图片保存品质*/ makethumb("a.jpg","b.jpg","50","50");function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$words=null,$image=null) { $data = GetImageSize($srcFile); switch($data[2]) { case 1: $im=@ImageCreateFromGIF($srcFile); break; case 2: $im=@ImageCreateFromJPEG($srcFile); break; case 3: $im=@ImageCreateFromPNG($srcFile); break; } if(!$im) return False; $srcW=ImageSX($im); $srcH=ImageSY($im); $dstX=0; $dstY=0; if ($srcW*$dstH>$srcH*$dstW) { $fdstH = round($srcH*$dstW/$srcW); $dstY = floor(($dstH-$fdstH)/2); $fdstW = $dstW; } else { $fdstW = round($srcW*$dstH/$srcH); $dstX = floor(($dstW-$fdstW)/2); $fdstH = $dstH; } $ni=ImageCreateTrueColor($dstW,$dstH); $dstX=($dstX<0)?0:$dstX; $dstY=($dstX<0)?0:$dstY; $dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX; $dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY; $white = ImageColorAllocate($ni,255,255,255); $black = ImageColorAllocate($ni,0,0,0); imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色 ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH); if($words!=null) { $words=iconv("gb2312","UTF-8",$words); //转换文字编码 ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$words); //写入文字水印 //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容 } elseif($image!=null) { $wimage_data = GetImageSize($image); switch($wimage_data[2]) { case 1: $wimage=@ImageCreateFromGIF($image); break; case 2: $wimage=@ImageCreateFromJPEG($image); break; case 3: $wimage=@ImageCreateFromPNG($image); break; } imagecopy($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31 imagedestroy($wimage); } ImageJpeg($ni,$dstFile,$rate); ImageJpeg($ni,$srcFile,$rate); imagedestroy($im); imagedestroy($ni); } ?> GD库弄好了。直接在PHP页面调用这个类就行了吗?我现在页面读出来的是大的原图。 对,在php.ini 配置文件中 把 gd2 的配置打开,就可以了,然后上网搜 php 缩略图, 很多的,就可以用了,写个方法,调用就 OK 了 <?php /* * $o_photo 原图路径 * $d_photo 处理后图片路径 * $width 定义宽 * $height 定义高 * 调用方法 cutphoto("test.jpg","temp.jpg",256,146); */ function cutphoto($o_photo,$d_photo,$width,$height){ $temp_img = imagecreatefromjpeg($o_photo); $o_width = imagesx($temp_img); //取得原图宽 $o_height = imagesy($temp_img); //取得原图高 //判断处理方法 if($width>$o_width || $height>$o_height){ //原图宽或高比规定的尺寸小,进行压缩 $newwidth=$o_width; $newheight=$o_height; if($o_width>$width){ $newwidth=$width; $newheight=$o_height*$width/$o_width; } if($newheight>$height){ $newwidth=$newwidth*$height/$newheight; $newheight=$height; } //缩略图片 $new_img = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height); imagejpeg($new_img , $d_photo); imagedestroy($new_img); }else{ //原图宽与高都比规定尺寸大,进行压缩后裁剪 if($o_height*$width/$o_width>$height){ //先确定width与规定相同,如果height比规定大,则ok $newwidth=$width; $newheight=$o_height*$width/$o_width; $x=0; $y=($newheight-$height)/2; }else{ //否则确定height与规定相同,width自适应 $newwidth=$o_width*$height/$o_height; $newheight=$height; $x=($newwidth-$width)/2; $y=0; } //缩略图片 $new_img = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height); imagejpeg($new_img , $d_photo); imagedestroy($new_img); $temp_img = imagecreatefromjpeg($d_photo); $o_width = imagesx($temp_img); //取得缩略图宽 $o_height = imagesy($temp_img); //取得缩略图高 //裁剪图片 $new_imgx = imagecreatetruecolor($width,$height); imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height); imagejpeg($new_imgx , $d_photo); imagedestroy($new_imgx); } } ?>找了1个源码,但是使用的时候没有生成新的图片是怎么回事?我是这样套用的:$filepath = "/var/www/html/test/images/" . $_FILES["file"]["name"];$smallpath = "/var/www/html/test/images/small/" . $_FILES["file"]["name"];cutphoto($filepath,$smallpath,100,100); 根据条件截取某部份的字符 php如何得到客房端的外网地址?? 关于类方法显示问题 thinkphp中模板显示问题 请问Out of memory是什么问题? 利用PHP进行视频转换(急救) 上传图片的时候出现这样的错误提示,大家帮忙看看! 怎样去掉一个字串中的html标签? 请问各位大哥,网上有没有好的PHP教程? git创建分支的时候提示not a valid name 请教IIS的伪静态规则 php计算某个网站的根目录文件夹的大小
<?php
/*构造函数-生成缩略图+水印,参数说明:
$srcFile-图片文件名,
$dstFile-另存文件名,
$words-水印文字,
$image-水印图片,
$dstW-图片保存宽度,
$dstH-图片保存高度,
$rate-图片保存品质*/
makethumb("a.jpg","b.jpg","50","50");
function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$words=null,$image=null)
{
$data = GetImageSize($srcFile);
switch($data[2])
{
case 1:
$im=@ImageCreateFromGIF($srcFile);
break;
case 2:
$im=@ImageCreateFromJPEG($srcFile);
break;
case 3:
$im=@ImageCreateFromPNG($srcFile);
break;
}
if(!$im) return False;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH>$srcH*$dstW)
{
$fdstH = round($srcH*$dstW/$srcW);
$dstY = floor(($dstH-$fdstH)/2);
$fdstW = $dstW;
}
else
{
$fdstW = round($srcW*$dstH/$srcH);
$dstX = floor(($dstW-$fdstW)/2);
$fdstH = $dstH;
}
$ni=ImageCreateTrueColor($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$white = ImageColorAllocate($ni,255,255,255);
$black = ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
if($words!=null)
{
$words=iconv("gb2312","UTF-8",$words);
//转换文字编码
ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$words); //写入文字水印
//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}
elseif($image!=null)
{
$wimage_data = GetImageSize($image);
switch($wimage_data[2])
{
case 1:
$wimage=@ImageCreateFromGIF($image);
break;
case 2:
$wimage=@ImageCreateFromJPEG($image);
break;
case 3:
$wimage=@ImageCreateFromPNG($image);
break;
}
imagecopy($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31
imagedestroy($wimage);
}
ImageJpeg($ni,$dstFile,$rate);
ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>
/*
* $o_photo 原图路径
* $d_photo 处理后图片路径
* $width 定义宽
* $height 定义高
* 调用方法 cutphoto("test.jpg","temp.jpg",256,146);
*/ function cutphoto($o_photo,$d_photo,$width,$height){ $temp_img = imagecreatefromjpeg($o_photo);
$o_width = imagesx($temp_img); //取得原图宽
$o_height = imagesy($temp_img); //取得原图高 //判断处理方法
if($width>$o_width || $height>$o_height){ //原图宽或高比规定的尺寸小,进行压缩 $newwidth=$o_width;
$newheight=$o_height; if($o_width>$width){
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
} if($newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
} //缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
}else{ //原图宽与高都比规定尺寸大,进行压缩后裁剪 if($o_height*$width/$o_width>$height){ //先确定width与规定相同,如果height比规定大,则ok
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{ //否则确定height与规定相同,width自适应
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0;
} //缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
$temp_img = imagecreatefromjpeg($d_photo);
$o_width = imagesx($temp_img); //取得缩略图宽
$o_height = imagesy($temp_img); //取得缩略图高 //裁剪图片
$new_imgx = imagecreatetruecolor($width,$height);
imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
imagejpeg($new_imgx , $d_photo);
imagedestroy($new_imgx);
} }
?>找了1个源码,但是使用的时候没有生成新的图片是怎么回事?
我是这样套用的:
$filepath = "/var/www/html/test/images/" . $_FILES["file"]["name"];
$smallpath = "/var/www/html/test/images/small/" . $_FILES["file"]["name"];
cutphoto($filepath,$smallpath,100,100);