好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>后端语言>PHP
<tfoot draggable='sEl'></tfoot>

图片压缩函数php php压缩上传图片

很多站长朋友们都不太清楚图片压缩函数php,今天小编就来给大家整理图片压缩函数php,希望对各位有所帮助,具体内容如下:

本文目录一览: 1、 php将pdf文件格式转换成图片,并压缩 2、 php 调整图片大小函数 3、 PHP网站上传图片自动压缩,怎么编程啊,求指 4、 PHP等比例压缩图片的实例代码 5、 php 怎么压缩图片的大小 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有这样的函数吗,我不太清楚,如果有,是不是要加载一系列的DLL呢,我建议不要用PHP的函数来做这个。

我建议使用“ImageMagick”工具来做这些事情,中文网站:

ImageMagick可以做许许多多的功能:它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线,多边形,椭圆,曲线,附加到图片伸展旋转。ImageMagick是免费软件:全部源码开放,可以自由使用,复制,修改,发布,它遵守GPL许可协议,可以运行于大多数的操作系统。ImageMagick的大多数功能的使用都来源于命令行工具。通常来说,它可以支持以下程序语言: Perl, C, C++, Python, PHP, Ruby, Java;现成的ImageMagick接口(PerlMagick, Magick++, PythonMagick, MagickWand for PHP, RubyMagick, and JMagick)是可利用的。这使得自动的动态的修改创建图片变为可能。ImageMagick支持至少90种图片格式:。

图片转换的核心文件是CONVERT.EXE,利用这个文件,使用PHP的SYSTEM调用,可以完成各种图片文件的变换。我网站的程序里面把用户上传图片宽度大于800的调整为800的语句如下:

$imginfo=getimagesize($tmp_name);

if ($imginfo $imginfo[0]>0){

//压缩宽度超过800的图片为800,10K以上的JPG图片用30%进行压缩

if ($imginfo[0]>800){

echo "你上传的图片幅面为 $imginfo[0] x $imginfo[1] ,将被压缩到800个象素宽。<br>";

system('"C:\Program Files\ImageMagick-6.0.7-Q16\convert.exe"'." -sample 800 -quality 80 $tmp_name $tmp_name");

} elseif($imginfo[2]==2 $src_size>10240) system('"C:\Program Files\ImageMagick-6.0.7-Q16\convert.exe"'." -quality 30 $tmp_name $tmp_name");

//压缩BMP、TIFF文件为JPG格式

if ($imginfo[2]==6 || $imginfo[2]==7 || $imginfo[2]==8){

system('"C:\Program Files\ImageMagick-6.0.7-Q16\convert.exe"'." -quality 80 $tmp_name $tmp_name.jpg");

rename("$tmp_name.jpg",$tmp_name);

$ext='jpg';

}

}

PHP网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

connect.php:连接数据库

test_upload.php:执行SQL语句

upload_img.php:上传图片并压缩

三个文件代码如下:

连接数据库:connect.php

<?php

$db_host = '';

$db_user = '';

$db_psw = '';

$db_name = '';

$db_port = '';

$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);

$q="set names utf8;";

$result=$sqlconn->query($q);

if (mysqli_connect_errno()) {

 printf("Connect failed: %s\n", mysqli_connect_error());

 exit();

}

?>

当然使用一些封装的数据库类也是可以的。

执行SQL语句:test_upload.php

<?php

require ("connect.php");

require ("upload_img.php");

$real_img=$uploadfile; 

$small_img=$uploadfile_resize;

$insert_sql = "insert into img (real_img,small_img) values (?,?)";

$result = $sqlconn -> prepare($insert_sql);

$result -> bind_param("ss", $real_img,$small_img);

$result -> execute();

?>

上传图片并压缩:upload_img.php

<?php 

//设置文件保存目录

$uploaddir = "/upfiles/"; 

//设置允许上传文件的类型

$type=array("jpg","gif","bmp","jpeg","png"); 

 

//获取文件后缀名函数 

function fileext($filename) 

{ 

 return substr(strrchr($filename, '.'), 1); 

} 

 

//生成随机文件名函数 

function random($length) 

{ 

 $hash = 'CR-'; 

 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; 

 $max = strlen($chars) - 1; 

 mt_srand((double)microtime() * 1000000); 

 for($i = 0; $i < $length; $i++) 

 { 

  $hash .= $chars[mt_rand(0, $max)]; 

 } 

 return $hash; 

} 

 

$a=strtolower(fileext($_FILES['filename']['name'])); 

 

//判断文件类型 

if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)) 

{ 

 $text=implode(",",$type); 

 $ret_code=3;//文件类型错误

 $page_result=$text;

 $retArray = array('ret_code' => $ret_code,'page_result'=>$page_result);

 $retJson = json_encode($retArray);

 echo $retJson;

 return;

} 

 

//生成目标文件的文件名 

else

{ 

 $filename=explode(".",$_FILES['filename']['name']); 

 do

 { 

  $filename[0]=random(10); //设置随机数长度 

  $name=implode(".",$filename); 

  //$name1=$name.".Mcncc"; 

  $uploadfile=$uploaddir.$name; 

 } 

 

 while(file_exists($uploadfile)); 

 

 if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)) 

 { 

  if(is_uploaded_file($_FILES['filename']['tmp_name'])) 

  {

   $ret_code=1;//上传失败

  } 

 else

 {//上传成功

  $ret_code=0;

 } 

 } 

$retArray = array('ret_code' => $ret_code);

$retJson = json_encode($retArray);

echo $retJson;

}

 

//压缩图片

 

$uploaddir_resize="/upfiles_resize/";

$uploadfile_resize=$uploaddir_resize.$name;

 

//$pic_width_max=120;

//$pic_height_max=90;

//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩

 

$file_type=$_FILES["filename"]['type'];

 

function ResizeImage($uploadfile,$maxwidth,$maxheight,$name)

{

 //取得当前图片大小

 $width = imagesx($uploadfile);

 $height = imagesy($uploadfile);

 $i=0.5;

 //生成缩略图的大小

 if(($width > $maxwidth) || ($height > $maxheight))

 {

  /*

  $widthratio = $maxwidth/$width;

  $heightratio = $maxheight/$height;

   

  if($widthratio < $heightratio)

  {

   $ratio = $widthratio;

  }

  else

  {

    $ratio = $heightratio;

  }

   

  $newwidth = $width * $ratio;

  $newheight = $height * $ratio;

  */

  $newwidth = $width * $i;

  $newheight = $height * $i;

  if(function_exists("imagecopyresampled"))

  {

   $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);

   imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  }

  else

  {

   $uploaddir_resize = imagecreate($newwidth, $newheight);

   imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  }

   

  ImageJpeg ($uploaddir_resize,$name);

  ImageDestroy ($uploaddir_resize);

 }

 else

 {

  ImageJpeg ($uploadfile,$name);

 }

}

 

 

 

if($_FILES["filename"]['size'])

{

 if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg")

 {

  //$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);

  $im = imagecreatefromjpeg($uploadfile);

 }

 elseif($file_type == "image/x-png")

 {

  //$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);

  $im = imagecreatefromjpeg($uploadfile);

 }

 elseif($file_type == "image/gif")

 {

  //$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);

  $im = imagecreatefromjpeg($uploadfile);

 }

 else//默认jpg

 {

  $im = imagecreatefromjpeg($uploadfile);

 }

 if($im)

 {

  ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

  

  ImageDestroy ($im);

 }

} 

?>

请按照现实情况更改connect.php,test_upload.php中对应的信息。

望采纳,谢谢。

PHP等比例压缩图片的实例代码

具体代码如下所示:

/**

*

desription

压缩图片

*

@param

sting

$imgsrc

图片路径

*

@param

string

$imgdst

压缩后保存路径

*/

public

function

compressedImage($imgsrc,

$imgdst)

{

list($width,

$height,

$type)

=

getimagesize($imgsrc);

$new_width

=

$width;//压缩后的图片宽

$new_height

=

$height;//压缩后的图片高

if($width

>=

600){

$per

=

600

/

$width;//计算比例

$new_width

=

$width

*

$per;

$new_height

=

$height

*

$per;

}

switch

($type)

{

case

1:

$giftype

=

check_gifcartoon($imgsrc);

if

($giftype)

{

header('Content-Type:image/gif');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefromgif($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

}

break;

case

2:

header('Content-Type:image/jpeg');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefromjpeg($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

break;

case

3:

header('Content-Type:image/png');

$image_wp

=

imagecreatetruecolor($new_width,

$new_height);

$image

=

imagecreatefrompng($imgsrc);

imagecopyresampled($image_wp,

$image,

0,

0,

0,

0,

$new_width,

$new_height,

$width,

$height);

//90代表的是质量、压缩图片容量大小

imagejpeg($image_wp,

$imgdst,

90);

imagedestroy($image_wp);

imagedestroy($image);

break;

}

}

总结

以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP

实现等比压缩图片尺寸和大小实例代码php

gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例

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的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。

查看更多关于图片压缩函数php php压缩上传图片的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did209205
更新时间:2023-05-03   阅读:19次

上一篇: phpstrstr乱码 php汉字乱码

下一篇:php循环语句编写 php的循环语句

相关资讯

最新资料更新

  • 1.php网站后台demo php后端模板
  • 2.php页面加ico php嵌入网页
  • 3.php有类似cmap 与php类似的语言
  • 4.订餐系统php 订餐系统升级维护,请前往
  • 5.php限制访问ip php访问被拒绝
  • 6.php上传图片木马 php图片上传代码
  • 7.php网络通信 php通信协议
  • 8.php登录和注册 php登录和注册不使用数据库
  • 9.php队列和缓存 php中的九大缓存技术
  • 10.php教务系统网页设计 php教务系统网页设计方案
  • 11.php求数组个数 php求数组的和
  • 12.php浏览页面乱码 php页面字符出现乱码怎么解决
  • 13.php类方法 php类方法访问变量
  • 14.php本地myaql工具 phpmysqlnd
  • 15.包含php-fpm-t的词条
  • 16.phpsessions phpsession使用
  • 17.php扩展的路径 php常用扩展有哪些
  • 18.php函数rand PHP函数的参数传递包括
  • 19.php根据城市定位 php获取位置信息
  • 20.php和php-fpm的简单介绍

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://www.haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]