色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP中改變圖片的尺寸大小的代碼

先介紹一個(gè)自己寫(xiě)的函數(shù)。
復(fù)制代碼 代碼如下:
<?php
$imgsrc = "http://www.nowamagic.NET/images/3.jpg";
$width = 780;
$height = 420;
resizejpg($imgsrc,$imgdst,$width,$height);
function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight)
{
//$imgsrc jpg格式圖像路徑 $imgdst jpg格式圖像保存文件名 $imgwidth要改變的寬度 $imgheight要改變的高度
//取得圖片的寬度,高度值
$arr = getimagesize($imgsrc);
header("Content-type: image/jpg");
$imgWidth = $imgwidth;
$imgHeight = $imgheight;
// Create image and define colors
$imgsrc = imagecreatefromjpeg($imgsrc);
$image = imagecreatetruecolor($imgWidth, $imgHeight); //創(chuàng)建一個(gè)彩色的底圖
imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
imagepng($image);
imagedestroy($image);
}
?>

imagecopyresampled
imagecopyresampled -- 重采樣拷貝部分圖像并調(diào)整大小。
int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
imagecopyresampled() 將一幅圖像中的一塊正方形區(qū)域拷貝到另一個(gè)圖像中,平滑地插入像素值,因此,尤其是,減小了圖像的大小而仍然保持了極大的清晰度。dst_im 和 src_im 分別是目標(biāo)圖像和源圖像的標(biāo)識(shí)符。如果源和目標(biāo)的寬度和高度不同,則會(huì)進(jìn)行相應(yīng)的圖像收縮和拉伸。坐標(biāo)指的是左上角。本函數(shù)可用來(lái)在同一幅圖內(nèi)部拷貝(如果 dst_im 和 src_im 相同的話)區(qū)域,但如果區(qū)域交迭的話則結(jié)果不可預(yù)知。
注: 因?yàn)檎{(diào)色板圖像限制(255+1 種顏色)有個(gè)問(wèn)題。重采樣或過(guò)濾圖像通常需要多于 255 種顏色,計(jì)算新的被重采樣的像素及其顏色時(shí)采用了一種近似值。對(duì)調(diào)色板圖像嘗試分配一個(gè)新顏色時(shí),如果失敗我們選擇了計(jì)算結(jié)果最接近(理論上)的顏色。這并不總是視覺(jué)上最接近的顏色。這可能會(huì)產(chǎn)生怪異的結(jié)果,例如空白(或者視覺(jué)上是空白)的圖像。要跳過(guò)這個(gè)問(wèn)題,請(qǐng)使用真彩色圖像作為目標(biāo)圖像,例如用 imagecreatetruecolor() 創(chuàng)建的。
注: imagecopyresampled() 需要 GD 2.0.l 或更高版本。
一個(gè)簡(jiǎn)單的示例:
復(fù)制代碼 代碼如下:
<?php
// The file
$filename = 'test.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;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>

示例2:
復(fù)制代碼 代碼如下:
<?php
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>

有兩種改變圖像大小的方法:
ImageCopyResized() 函數(shù)在所有GD版本中有效,但其縮放圖像的算法比較粗糙。
ImageCopyResamples(),其像素插值算法得到的圖像邊緣比較平滑。(但該函數(shù)的速度比 ImageCopyResized() 慢)。
兩個(gè)函數(shù)的參數(shù)是一樣的,如下:
復(fù)制代碼 代碼如下:
imageCopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);
imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

例子:
復(fù)制代碼 代碼如下:
<?php
$src = ImageCreateFromJPEG('php.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $widht/2;
$y = $height/2;
$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height);
header('Content-Type : image/png');
ImagePNG($det);
?>

php中如何改變jpg圖像文件的尺寸大小
復(fù)制代碼 代碼如下:
<?
function resize_jpg($img,$w,$h){
// $thumb = imagecreate ($w, $h);
$image = imagecreatefromjpeg($img);
$imagedata = getimagesize($img);
if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);//根據(jù)原圖的縱橫比得出高度!
$thumb = imagecreatetruecolor ($w, $h);
imagecopyresized ($thumb, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1]);
imagejpeg($thumb);
}
//resize_jpg($file,$w,$h);
resize_jpg("images/dsc01244.jpg",100,100);
imagedestory($thumb);
imagedestory($image);
?>

函數(shù)代碼:
復(fù)制代碼 代碼如下:
<?php
/*
* 圖片縮略圖
* $srcfile 來(lái)源圖片,
* $rate 縮放比,默認(rèn)為縮小一半,或者具體寬度象素值
* $filename 輸出圖片文件名jpg
* 例如: resizeimage("zt32.gif",0.1);
* 例如: resizeimage("zt32.gif",250 );
* 說(shuō)明:調(diào)用時(shí)直接把函數(shù)的結(jié)果放在HTML文件IMG標(biāo)簽中的SRC屬性里
*/
function resizeimage($srcfile,$rate=.5, $filename = "" ){
$size=getimagesize($srcfile);
switch($size[2]){
case 1:
$img=imagecreatefromgif($srcfile);
break;
case 2:
$img=imagecreatefromjpeg($srcfile);
break;
case 3:
$img=imagecreatefrompng($srcfile);
break;
default:
exit;
}
//源圖片的寬度和高度
$srcw=imagesx($img);
$srch=imagesy($img);
//目的圖片的寬度和高度
if($size[0] <= $rate || $size[1] <= $rate){
$dstw=$srcw;
$dsth=$srch;
}else{
if($rate <= 1){
$dstw=floor($srcw*$rate);
$dsth=floor($srch*$rate);
}else {
$dstw=$rate;
$rate = $rate/$srcw;
$dsth=floor($srch*$rate);
}
}
//echo "$dstw,$dsth,$srcw,$srch ";
//新建一個(gè)真彩色圖像
$im=imagecreatetruecolor($dstw,$dsth);
$black=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,$dstw,$dsth,$black);
imagecopyresized($im,$img,0,0,0,0,$dstw,$dsth,$srcw,$srch);
// 以 JPEG 格式將圖像輸出到瀏覽器或文件
if( $filename ) {
//圖片保存輸出
imagejpeg($im, $filename );
}else {
//圖片輸出到瀏覽器
imagejpeg($im);
}
//釋放圖片
imagedestroy($im);
imagedestroy($img);
}
?>

php技術(shù)PHP中改變圖片的尺寸大小的代碼,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 封开县| 保德县| 伊宁市| 屏山县| 蒙阴县| 拉萨市| 航空| 湖口县| 平江县| 威海市| 苏尼特左旗| 宁乡县| 富源县| 德化县| 定安县| 昌江| 房山区| 顺昌县| 布尔津县| 津市市| 乃东县| 武平县| 南岸区| 安平县| 辽阳市| 蓬莱市| 南充市| 介休市| 运城市| 会昌县| 渭源县| 平定县| 威信县| 余江县| 宕昌县| 吴桥县| 遂昌县| 达孜县| 建德市| 湘潭县| 崇州市|