欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php缩略图生成几种方法代码_PHP教程

程序员文章站 2024-01-03 16:10:22
...
1. 从中我们可以看到imagecreatetruecolor函数的作用明显地是创建一幅黑色的背景图片,它的第一个参数为所创建图片的宽,第二个参数为所创建图片的高,我们把这个函数的返回值(图像标识符)存入变量里面。

2.imagecreatefromjpeg作用就是将要进行分割的图片读到内存里面(这里大家可能有纪疑问:我直接从硬微盘里读不就得了,为什么还要先读到内存里呢?打个不恰当的比方,大家平时在用钱的时相信大家不会口袋里不会放太多,一般到用的时候才从银行里面取,这里也是一样,这张图片不用它的时候我把它放在硬盘里面,当要对这张图片进行分割或其它操作时就把它读到内存里面,说白了,内存给程序提供了一个运行的舞台)

3.再看imagecopyresampled函数它的作用是将原图片分割好,然后将它和采样拷贝(我理解为投影)到用imagecreatefromjpeg创建好的背景图片上。

// The file
$filename = 'temp/Sunset.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

//以原图片的长宽的0.5为新的长宽来创建新的图片此图片的标志为$image_p
$image_p = imagecreatetruecolor($new_width, $new_height);
//从 JPEG文件或URL新建一图像
$image = imagecreatefromjpeg($filename);
//将原始图片从坐标(100,100)开始分割,分割的长度(400),高度为(300)原图片的一半,将分割好的图片放在从坐标(0,0)开始的已建好的区域里
imagecopyresampled($image_p, $image, 0, 0, 100, 100, $new_width, $new_height, 400, 300);

// Output
imagejpeg($image_p, null, 100);//quality为图片输出的质量范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大)。
?>

上面的例子是把$image图片从坐标(100,100)进行分割,分割后的宽为400,高为300,然后再将此图片从坐标(0,0)处开始投影到图片$image_p上,,投影的宽为$new_width,高为$new_height。

// 文件及缩放尺寸
//$imgfile = 'smp.jpg';
//$percent = 0.2;
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = ImageCreateTrueColor($newwidth,$newheight);
$source = imagecreatefromjpeg($imgfile);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
?>

更详细教程

/*构造函数-生成缩略图+水印,参数说明:
$srcFile-图片文件名,
$dstFile-另存文件名,
$markwords-水印文字,
$markimage-水印图片,
$dstW-图片保存宽度,
$dstH-图片保存高度,
$rate-图片保存品质*/
makethumb("a.jpg","b.jpg","50","50");
function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$markwords=null,$markimage=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 $dstY=($dstX $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($markwords!=null)
{
$markwords=iconv("gb2312","UTF-8",$markwords);
//转换文字编码
ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //写入文字水印
//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}
elseif($markimage!=null)
{
$wimage_data = GetImageSize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@ImageCreateFromGIF($markimage);
break;
case 2:
$wimage=@ImageCreateFromJPEG($markimage);
break;
case 3:
$wimage=@ImageCreateFromPNG($markimage);
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);
}
?>

实例四

$thumbnail = new ImageResize();
$thumbnail->resizeimage(源图片完整路径, 缩略图宽度, 缩略图高度, 是否剪裁(0或者1), 新图片完整路径);

class ImageResize {

//图片类型
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 $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 $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);
}

if($this->type=="bmp") {
$this->im = $this->imagecreatefrombmp($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;
}

function ConvertBMP2GD($src, $dest = false) {
if(!($src_f = fopen($src, "rb"))) {
return false;
}
if(!($dest_f = fopen($dest, "wb"))) {
return false;
}
$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));

extract($info);
extract($header);

if($type != 0x4D42) { // signature "BM"
return false;
}

$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$gd_header = "";
// true-color vs. palette
$gd_header .= ($palette_size == 0) ? "xFFxFE" : "xFFxFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "x01" : "x00";
if($palette_size) {
$gd_header .= pack("n", $ncolor);
}
// no transparency
$gd_header .= "xFFxFFxFFxFF";

fwrite($dest_f, $gd_header);

if($palette_size) {
$palette = fread($src_f, $palette_size);
$gd_palette = "";
$j = 0;
while($j $b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
$gd_palette .= "$r$g$b$a";
}
$gd_palette .= str_repeat("x00x00x00x00", 256 - $ncolor);
fwrite($dest_f, $gd_palette);
}

$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size &
0x03) : 0;

for($i = 0, $l = $height - 1; $i // BMP stores scan lines starting from bottom
fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_f, $scan_line_size);
if($bits == 24) {
$gd_scan_line = "";
$j = 0;
while($j $b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "x00$r$g$b";
}
}
else if($bits == 8) {
$gd_scan_line = $scan_line;
}
else if($bits == 4) {
$gd_scan_line = "";
$j = 0;
while($j $byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= "$p1$p2";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
else if($bits == 1) {
$gd_scan_line = "";
$j = 0;
while($j $byte = ord($scan_line{$j++});
$p1 = chr((int) (($byte & 0x80) != 0));
$p2 = chr((int) (($byte & 0x40) != 0));
$p3 = chr((int) (($byte & 0x20) != 0));
$p4 = chr((int) (($byte & 0x10) != 0));
$p5 = chr((int) (($byte & 0x08) != 0));
$p6 = chr((int) (($byte & 0x04) != 0));
$p7 = chr((int) (($byte & 0x02) != 0));
$p8 = chr((int) (($byte & 0x01) != 0));
$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
fwrite($dest_f, $gd_scan_line);
}
fclose($src_f);
fclose($dest_f);
return true;
}

function imagecreatefrombmp($filename) {
$tmp_name = tempnam("/tmp", "GD");
if($this->ConvertBMP2GD($filename, $tmp_name)) {
$img = imagecreatefromgd($tmp_name);
unlink($tmp_name);
return $img;
}
return false;
}

}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632981.htmlTechArticle1. 从中我们可以看到imagecreatetruecolor函数的作用明显地是创建一幅黑色的背景图片,它的第一个参数为所创建图片的宽,第二个参数为所创建图...

上一篇:

下一篇: