php縮圖產生幾種方法代碼_PHP教程

來源:互聯網
上載者:User
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<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($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<$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);
}

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 < $palette_size) {
$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 < $height; $i++, $l--) {
// 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 < $scan_line_size) {
$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 < $scan_line_size) {
$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 < $scan_line_size) {
$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;
}

}

http://www.bkjia.com/PHPjc/632981.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632981.htmlTechArticle1. 從中我們可以看到imagecreatetruecolor函數的作用明顯地是建立一幅黑色的背景圖片,它的第一個參數為所建立圖片的寬,第二個參數為所建立圖...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.