96编辑器旗下产品
找图
终身
ID:0
到期时间:永久
退出登录
您的位置:首页 > 百科管理 > 详情

php 图片压缩-如何利用php把上传的图片压缩

原创:找图网 2023-04-14 04:37:07
  • 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/ , 在24行断点

    可能会得到消息:

    然后,在百度下,原来是压缩的文件过大,插件使用的缓存不够,这里直接将配置改大即可

    将配置文件改成如下

  • php 怎么压缩图片的大小

  • php 压缩图片的大小:

    <?php

    $im = imagecreatefromjpeg('D:');

    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

    // The file

    $filename = '';

    $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);

    ?>

    最新文章 更多
    最新素材 更多
    公司介绍 网站地图 图片资讯
    Copyright © 2010-2022 山东找图网络科技有限公司  鲁ICP备18007836号-3  邮箱:15653358549@163.com