当前位置:Gxlcms > PHP教程 > PHP中改变图片的尺寸大小的代码_PHP教程

PHP中改变图片的尺寸大小的代码_PHP教程

时间:2021-07-01 10:21:17 帮助过:11人阅读

先介绍一个自己写的函数。
代码如下:
  1. <br><!--?php <BR-->$imgsrc = "http://www.nowamagic.net/images/3.jpg"; <br>$width = 780; <br>$height = 420; <br>resizejpg($imgsrc,$imgdst,$width,$height); <br>function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight) <br>{ <br>//$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度 <br>//取得图片的宽度,高度值 <br>$arr = getimagesize($imgsrc); <br>header("Content-type: image/jpg"); <br>$imgWidth = $imgwidth; <br>$imgHeight = $imgheight; <br>// Create image and define colors <br>$imgsrc = imagecreatefromjpeg($imgsrc); <br>$image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图 <br>imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]); <br>imagepng($image); <br>imagedestroy($image); <br>} <br>?> <br> <br>imagecopyresampled <br>imagecopyresampled -- 重采样拷贝部分图像并调整大小。 <br>int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH) <br>imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。dst_im 和 src_im 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_im 和 src_im 相同的话)区域,但如果区域交迭的话则结果不可预知。 <br>注: 因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用 imagecreatetruecolor() 创建的。 <br>注: imagecopyresampled() 需要 GD 2.0.l 或更高版本。 <br>一个简单的示例: <br><span style="CURSOR: pointer" onclick="doCopy('code56623')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->// The file <br>$filename = 'test.jpg'; <br>$percent = 0.5; <br>// Content type <br>header('Content-Type: image/jpeg'); <br>// Get new dimensions <br>list($width, $height) = getimagesize($filename); <br>$new_width = $width * $percent; <br>$new_height = $height * $percent; <br>// Resample <br>$image_p = imagecreatetruecolor($new_width, $new_height); <br>$image = imagecreatefromjpeg($filename); <br>imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); <br>// Output <br>imagejpeg($image_p, null, 100); <br>?> <br> <br>示例2: <br><span style="CURSOR: pointer" onclick="doCopy('code34596')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->// The file <br>$filename = 'test.jpg'; <br>// Set a maximum height and width <br>$width = 200; <br>$height = 200; <br>// Content type <br>header('Content-Type: image/jpeg'); <br>// Get new dimensions <br>list($width_orig, $height_orig) = getimagesize($filename); <br>$ratio_orig = $width_orig/$height_orig; <br>if ($width/$height > $ratio_orig) { <br>$width = $height*$ratio_orig; <br>} else { <br>$height = $width/$ratio_orig; <br>} <br>// Resample <br>$image_p = imagecreatetruecolor($width, $height); <br>$image = imagecreatefromjpeg($filename); <br>imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); <br>// Output <br>imagejpeg($image_p, null, 100); <br>?> <br> <br>有两种改变图像大小的方法: <br>ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。 <br>ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑。(但该函数的速度比 ImageCopyResized() 慢)。 <br>两个函数的参数是一样的,如下: <br><span style="CURSOR: pointer" onclick="doCopy('code74117')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>imageCopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh); <br>imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh); <br> <br>例子: <br><span style="CURSOR: pointer" onclick="doCopy('code55554')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?PHP <BR-->$src = ImageCreateFromJPEG('php.jpg'); <br>$width = ImageSx($src); <br>$height = ImageSy($src); <br>$x = $widht/2; <br>$y = $height/2; <br>$dst = ImageCreateTrueColor($x,$y); <br>ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height); <br>header('Content-Type : image/png'); <br>ImagePNG($det); <br>?> <br><br><strong>在php中如何改变jpg图像文件的尺寸大小 <br></strong><span style="CURSOR: pointer" onclick="doCopy('code37801')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--? <BR-->function resize_jpg($img,$w,$h){ <br>// $thumb = imagecreate ($w, $h); <br>$image = imagecreatefromjpeg($img); <br>$imagedata = getimagesize($img); <br>if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);//根据原图的纵横比得出高度! <br>$thumb = imagecreatetruecolor ($w, $h); <br>imagecopyresized ($thumb, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1]); <br>imagejpeg($thumb); <br>} <br>//resize_jpg($file,$w,$h); <br>resize_jpg("images/dsc01244.jpg",100,100); <br>imagedestory($thumb); <br>imagedestory($image); <br>?> <br> <br>函数代码: <br><span style="CURSOR: pointer" onclick="doCopy('code64504')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->/* <br>* 图片缩略图 <br>* $srcfile 来源图片, <br>* $rate 缩放比,默认为缩小一半,或者具体宽度象素值 <br>* $filename </li></ol></pre>输出图片文件名jpg <br>* 例如: resizeimage("zt32.gif",0.1); <br>* 例如: resizeimage("zt32.gif",250 ); <br>* 说明:调用时直接把函数的结果放在HTML文件IMG标签中的SRC属性里 <br>*/ <br>function resizeimage($srcfile,$rate=.5, $filename = "" ){ <br>$size=getimagesize($srcfile); <br>switch($size[2]){ <br>case 1: <br>$img=imagecreatefromgif($srcfile); <br>break; <br>case 2: <br>$img=imagecreatefromjpeg($srcfile); <br>break; <br>case 3: <br>$img=imagecreatefrompng($srcfile); <br>break; <br>default: <br>exit; <br>} <br>//源图片的宽度和高度 <br>$srcw=imagesx($img); <br>$srch=imagesy($img); <br>//目的图片的宽度和高度 <br>if($size[0] <= $rate || $size[1] <= $rate){ <br>$dstw=$srcw; <br>$dsth=$srch; <br>}else{ <br>if($rate <= 1){ <br>$dstw=floor($srcw*$rate); <br>$dsth=floor($srch*$rate); <br>}else { <br>$dstw=$rate; <br>$rate = $rate/$srcw; <br>$dsth=floor($srch*$rate); <br>} <br>} <br>//echo "$dstw,$dsth,$srcw,$srch "; <br>//新建一个真彩色图像 <br>$im=imagecreatetruecolor($dstw,$dsth); <br>$black=imagecolorallocate($im,255,255,255); <br>imagefilledrectangle($im,0,0,$dstw,$dsth,$black); <br>imagecopyresized($im,$img,0,0,0,0,$dstw,$dsth,$srcw,$srch); <br>// 以 JPEG 格式将图像</li></ol></pre>输出到浏览器或文件 <br>if( $filename ) { <br>//图片保存</li></ol></pre>输出 <br>imagejpeg($im, $filename ); <br>}else { <br>//图片</li></ol></pre>输出到浏览器 <br>imagejpeg($im); <br>} <br>//释放图片 <br>imagedestroy($im); <br>imagedestroy($img); <br>} <br>?> <br></li><li><p></p></li><li><p align="left"><span id="url" itemprop="url">http://www.bkjia.com/PHPjc/324010.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/324010.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">先介绍一个自己写的函数。 代码如下:</span></p><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>?php $imgsrc = "http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420; resizejpg($imgsrc,$imgdst,$width...<p></p></li><li> </li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行