php写的图片压缩类(php上传图片压缩)

发布时间:2022-11-08

本文目录一览:

  1. php 怎么压缩图片的大小
  2. php将pdf文件格式转换成图片,并压缩
  3. php 图片压缩显示
  4. PHP等比例压缩图片的实例代码

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, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
        } else {
            $newim = imagecreate($newwidth, $newheight);
            imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
        }
        $name = $name . $filetype;
        imagejpeg($newim, $name);
        imagedestroy($newim);
    } else {
        $name = $name . $filetype;
        imagejpeg($im, $name);
    }
}
?>

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 图片压缩显示

(1)网页结构里用:<img src="image.php?name=p01.png">,来调用处理后的图片信息。 (2)在后台脚本 image.php 里对传过来的图片名进行处理返回:

<?php
$pic = $_REQUEST['name'];
// 1. 打开图片源文件资源
$im = @imagecreatefrompng($pic);
if ($im) {
    // 2. 源文件的宽高,也可写为定值
    $fx = imagesx($im); // 取宽
    $fy = imagesy($im); // 取高
    // 3. 使用新的宽高
    $sx = 150;
    $sy = 100;
    // 4. 生成小图资源
    $sm = imagecreatetruecolor($sx, $sy);
    // 5. 进行缩放
    imagecopyresampled($sm, $im, 0, 0, 0, 0, $sx, $sy, $fx, $fy);
    // 6. 输出图像
    header('Content-Type: image/png');
    imagepng($sm);
    // 7. 释放资源
    imagedestroy($sm);
    imagedestroy($im);
}
?>

(3)代码里假设是对 png 图片处理,相关字都是 png,如果想对 jpg 类型处理的可都换成 jpeg。

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

具体代码如下所示:

/**
 * 压缩图片
 *
 * @param string $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模块实现图片缩放、裁剪、压缩示例