很多站长朋友们都不太清楚php+压缩+图片,今天小编就来给大家整理php+压缩+图片,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php 怎么压缩图片后 在发给前端 2、 php将pdf文件格式转换成图片,并压缩 3、 php 怎么压缩图片的大小 4、 PHP怎么对GIF动图进行压缩和上传 php 怎么压缩图片后 在发给前端<?php
/*
----------------------------------------------------------------------
函数:调整图片尺寸或生成缩略图
返回:True/False
参数:
$Image 需要调整的图片(含路径)
$Dw=450 调整时最大宽度;缩略图时的绝对宽度
$Dh=450 调整时最大高度;缩略图时的绝对高度
$Type=1 1,调整尺寸; 2,生成缩略图
$path='img/';//路径
$phtypes=array(
'img/gif',
'img/jpg',
'img/jpeg',
'img/bmp',
'img/pjpeg',
'img/x-png'
);
Function Img($Image,$Dw=450,$Dh=450,$Type=1){
IF(!File_Exists($Image)){
Return False;
}
//如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值
IF($Type!=1){
Copy($Image,Str_Replace(".","_x.",$Image));
$Image=Str_Replace(".","_x.",$Image);
}
//取得文件的类型,根据不同的类型建立不同的对象
$ImgInfo=GetImageSize($Image);
Switch($ImgInfo[2]){
Case 1:
$Img = @ImageCreateFromGIF($Image);
Break;
Case 2:
$Img = @ImageCreateFromJPEG($Image);
Break;
Case 3:
$Img = @ImageCreateFromPNG($Image);
Break;
}
//如果对象没有创建成功,则说明非图片文件
IF(Empty($Img)){
//如果是生成缩略图的时候出错,则需要删掉已经复制的文件
IF($Type!=1){Unlink($Image);}
Return False;
}
//如果是执行调整尺寸操作则
IF($Type==1){
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
IF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
}ElseIF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
}
}Else{
$width=$width;
$height=$height;
}
$nImg = ImageCreateTrueColor($width,$height); //新建一个真彩色画布
ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小
ImageJpeg ($nImg,$Image); //以JPEG格式将图像输出到浏览器或文件
Return True;
//如果是执行生成缩略图操作则
}Else{
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
$nImg = ImageCreateTrueColor($Dw,$Dh);
IF($h/$w>$Dh/$Dw){ //高比较大
$width=$Dw;
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
}Else{ //宽比较大
$height=$Dh;
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
}
ImageJpeg ($nImg,$Image);
Return True;
}
}
?>
<html><body>
<form method="post" enctype="multipart/form-data" name="form1">
<table>
<tr><td>上传图片</td></tr>
<tr><td><input type="file" name="photo" size="20" /></td></tr>
<tr><td><input type="submit" value="上传"/></td></tr>
</table>
允许上传的文件类型为:<?=implode(', ',$phtypes)?></form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if (!is_uploaded_file($_FILES["photo"][tmp_name])){
echo "图片不存在";
exit();
}
if(!is_dir('img')){//路径若不存在则创建
mkdir('img');
}
$upfile=$_FILES["photo"];
$pinfo=pathinfo($upfile["name"]);
$name=$pinfo['basename'];//文件名
$tmp_name=$upfile["tmp_name"];
$file_type=$pinfo['extension'];//获得文件类型
$showphpath=$path.$name;
if(in_array($upfile["type"],$phtypes)){
echo "文件类型不符!";
exit();
}
if(move_uploaded_file($tmp_name,$path.$name)){
echo "成功!";
Img($showphpath,100,800,2);
}
echo "<img src=\"".$showphpath."\" />";
}
?>
</body>
</html>
php将pdf文件格式转换成图片,并压缩有一份pdf文件,需要将其转换成图片, 如果图片过大,同时还需要将其压缩。
1、安装插件
因为不同版本的用法略有区别,我这里用的是2.1版, 最近文档还需要看官方文档。
2、简单使用
3、常用方法
4、其他
1、说明
2、安装
不同版本的使用略有区别,我这用的是2.5版本的
3、简单使用
其中resize,可以指定压缩的宽度和高度,如
如果是指定宽度,智适应高度就是这样
save的三个参数是,
4、更多
更多使用,看 说明文档
压缩图片的时候,报不能读取问题
这个可能是遇到最多的问题。可能原因如下:
1、文件读取权限
查看文件的权限,看是否有读取的权限(r), 没有的话直接把文件改为 777
2、插件不支持该格式文件
输入 php --ri imagick , 在支持列表看是否支持该文件的格式。没有的话,自己百度啦。
3、内存或缓存不够
进入插件的 /vendor/intervention/image/src/Intervention/Image/Imagick/Decoder.php , 在24行断点
可能会得到消息:
然后,在百度下,原来是压缩的文件过大,插件使用的缓存不够,这里直接将配置改大即可
将配置文件改成如下
php 怎么压缩图片的大小php 压缩图片的大小:
<?php
$im = imagecreatefromjpeg('D:phpplace.jpeg');
resizeImage($im,,,'xinde','.jpg');
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
echo "start-----------------" ;
if(($maxwidth $pic_width > $maxwidth) ($maxheight $pic_height > $maxheight))
{
if($maxwidth $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}
if($maxheight $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}
if($resizewidth_tag $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if($resizewidth_tag !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
PHP怎么对GIF动图进行压缩和上传单帧 gif 可以转换成 jpg/png/webp 等其他图片格式来节约体积。
多帧 gif 可以使用 gifsicle 压缩,也可以转换成 apng/webp 来节约体积,国外比较流行的一种做法是把 gif 转换成 mp4视频。
用哪种方法取决于你的用户端,压缩的效果可以通过 compression ratio/ssim/psnr 等来衡量,寻找一个合适的压缩参数。
如果用到的库没有 PHP binding,那么在条件允许的情况下可以通过 popen() 和标准流来集成。
关于php+压缩+图片的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php+压缩+图片 php压缩图片代码的详细内容...