很多站长朋友们都不太清楚php图片缩放类,今天小编就来给大家整理php图片缩放类,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php如何实时缩小图片大小 2、 如何实现PHP图片裁剪与缩放 3、 求php图片缩放处理函数 4、 php实现图片等比例缩放代码 5、 php图片可以等比例的缩放吗 php如何实时缩小图片大小PHP中缩放图像:
有两种改变图像大小的方法.
(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.
(2):ImageCopyResampled(),其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比
ImageCopyResized() 慢).
两个函数的参数是一样的.如下:
ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
它们两个都是从原图像(source)中抓取特定位置(sx,sy)复制图像qu区域到目标t
图像(destination)的特定位置(dx,dy)。另外dw,dh指定复制的图像区域在目标图像上的大小,sw,sh指定从原图像复制的图像区域
的大小。如果有ps经验的话,就相当于在原图像选择一块区域,剪切移动到目的图像上,同时有拉伸或缩小的操作。
例一:
(本例子是将图片按原大小的4/1的大小显示)
<?php
// 指定文件路径和缩放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定头文件Content typezhi值
header('Content-type: image/jpeg');
// 获取图片的宽高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = imagecreatefromjpeg($filename);
// 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 输出给浏览器
imagejpeg($thumb);
?>
如何实现PHP图片裁剪与缩放给你段代码吧。上边的是具体的事务处理。下面的是类文件。
////////////////////////////////////////////////////下面开始处理图片压缩问题
$src = "$fileurl";
echo $src;
$image = new Image($src);
$width= $image->getimgwidth();
echo $width."宽度是这些";
if($width>1024){
$coefficient=number_format(1024/$width, 4, '.', '');
echo $coefficient;
$image->percent = $coefficient;
$image->openImage();
$image->thumpImage();
//************************************重新给图片命名
$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;
echo "<br/>重新命名是这个".$randname;
$fileurl=$fileimgweb.$randname;//重新给数据库里存的图片地址命名
// $image->showImage();
$image->saveImage($fileurl);
}
////////////////////////////////////////////////////图片压缩问题处理结束
--------------------------------------------------------------------------------------
<?php
/**
图片压缩操作类
v1.0
*/
class Image{
private $src;
private $imageinfo;
private $image;
public $percent = 0.5;
public function __construct($src){
$this->src = $src;
}
/**
获取图片的宽度并传输给前台
*/
public function getimgwidth(){
$imgwidth= getimagesize($this->src)[0];
// echo $imgwidth;
return $imgwidth;
}
/**
打开图片
*/
public function openImage(){
list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);
}
/**
操作图片
*/
public function thumpImage(){
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
输出图片
*/
public function showImage(){
header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);
}
/**
保存图片到硬盘
*/
public function saveImage($fileurl){
imagejpeg($this->image, $fileurl,75);
// $funcs = "image".$this->imageinfo['type'];
// $funcs($this->image,$name.'.'.$this->imageinfo['type']);
}
/**
销毁图片
*/
public function destruct(){
imagedestroy($this->image);
}
}
?>
求php图片缩放处理函数<?php
/**
* 图片缩放
* @param string $url
* @param int $maxWidth
* @param int $maxHeight
* @return string
*/
function thumb($url, $maxWidth, $maxHeight, $info) {
$info = $imgInfo = getimagesize($url);
$width = $imgInfo[0];//获取图片宽度
$height = $imgInfo[1];//获取图片高度
$r = min($maxHeight/$height, $maxWidth/$width);
if($r >= 1) { // 不用缩放
$maxHeight = $height;
$maxWidth = $width;
} elseif($r < 1) { // 缩放
$maxHeight = $height * $r;
$maxWidth = $width * $r;
}
$temp_img = imagecreatetruecolor($maxWidth,$maxHeight); //创建画布
$fun = str_replace('/', 'createfrom', $imgInfo['mime']);
$im = $fun($url);
imagecopyresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun = str_replace('/', '', $imgInfo['mime']);
$fun($temp_img);
$imgstr = ob_get_contents();
ob_end_clean();
imagedestroy($im);
return $imgstr;
}
$imgUrl = $_GET['url'];
$info = array();
$string = thumb($imgUrl, 500, 500, $info);
$mimeArray = explode("/", $info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo $string;
以上代码存为thumb.php,调用效果:
php实现图片等比例缩放代码新建文件index.php,需要在统计目录下有个图片为q.jpg(可根据源码进行更改图片的名称)
源代码如下:
<?php
$filename="q.jpg";
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整
imagecopyresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//图像输出新图片、另存为
imagejpeg($new,
"q1.jpg");
imagedestroy($new);
imagedestroy($img);
?>
使用浏览器运行过后,在index.php同级的目录下会有个q1.jpg,这个图片就是等比例缩放后的图片,路径可以自己在源代码里面更改,放在自己的项目当中去或写个方法也行
以上所述上就是本文的全部内容了,希望对大家学习php语言能够有所帮助。
php图片可以等比例的缩放吗可以。
等比例缩放的方法是:
1、载入选区--自由变换。如下图:
2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。
关于php图片缩放类的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php图片缩放类 php图片放大缩放的详细内容...