解决php剪切缩略图生成png,PHP缩略图

发布时间:2022-11-21

本文目录一览:

  1. PHP 下载图片转换格式的问题?
  2. php创建缩略图问题
  3. 谁有php批量处理图片、图片生成缩略图、图片添加水印的函数?
  4. php怎么生成缩略图

PHP 下载图片转换格式的问题?

你需要 PHP 的 GD 扩展组件来转换 png/gif 到 jpg。 注意 jpg 会忽略 alpha(透明度),下面的函数将背景默认为白色,压缩设置为 80%。 函数有两个参数:$file = 要转换的 png/gif 文件,$jpg = 输出的 jpg 文件。

function img2jpg($file, $jpg) {
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    if ($ext == "png")
        $image = imagecreatefrompng($file);
    else if ($ext == "gif")
        $image = imagecreatefromgif($file);
    else
        return true;
    $bg = imagecreatetruecolor(imagesx($image), imagesy($image));
    imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
    imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
    imagedestroy($image);
    imagejpeg($bg, $jpg, 80);
    ImageDestroy($bg);
}
img2jpg("image.png", "image.jpg");

如果你运行上面的代码后出现 Call to undefined function imagecreatefrompng() 类似的错误,那应该是 PHP 没有开启 GD 扩展组件。

php创建缩略图问题

其实PHP创建缩略图就是在PHP在原图片的基础上创建一张新的图片的过程,而用PHP创建图像的过程一般分成四步:

  1. 创建一张画布(只要是画图都需要一张画布)
  2. 在画布上画东西(可以画各种图形,如长方形、直线等,也可以在画布上写字或画其他图形)
  3. 画完图之后,将图片输出,将图片输出到浏览器显示出来,或者保存为一张新的图片(缩略图一般是保存为图片文件的)
  4. 因为创建画布时打开了文件流,所以要关闭资源,节省内存。 具体的代码如下(这段代码来源于 ThinkPHP 的图像类):
class Thumb {
    /**
     * @param string $img 原图
     * @param string $thumbname 缩略图文件名
     * @param string $type 图像格式
     * @param string $maxWidth 宽度
     * @param string $maxHeight 高度
     */
    static function create($img, $thumbname, $type = '', $maxWidth = 200, $maxHeight = 50) {
        $info = getimagesize($img); // 获取原图的图像信息(长、宽、格式等)
        if ($info !== false) {
            $srcWidth = $info['width'];
            $srcHeight = $info['height'];
            $type = empty($type) ? $info['type'] : $type;
            $type = strtolower($type);
            $interlace = $interlace ? 1 : 0;
            unset($info);
            $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
            if ($scale >= 1) {
                // 超过原图大小不再缩略
                $width = $srcWidth;
                $height = $srcHeight;
            } else {
                // 缩略图尺寸
                $width = (int)($srcWidth * $scale);
                $height = (int)($srcHeight * $scale);
            }
            // 载入原图(在原图的基础上创建画布,为第一步)
            $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
            if (!function_exists($createFun)) {
                return false;
            }
            $srcImg = $createFun($img);
            // 创建缩略图
            if ($type != 'gif' && function_exists('imagecreatetruecolor'))
                $thumbImg = imagecreatetruecolor($width, $height);
            else
                $thumbImg = imagecreate($width, $height);
            // png和gif的透明处理 by luofei614
            if ('png' == $type) {
                imagealphablending($thumbImg, false); // 取消默认的混色模式(为解决阴影为绿色的问题)
                imagesavealpha($thumbImg, true); // 设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
            } elseif ('gif' == $type) {
                $trnprt_indx = imagecolortransparent($srcImg);
                if ($trnprt_indx >= 0) {
                    // its transparent
                    $trnprt_color = imagecolorsforindex($srcImg, $trnprt_indx);
                    $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                    imagefill($thumbImg, 0, 0, $trnprt_indx);
                    imagecolortransparent($thumbImg, $trnprt_indx);
                }
            }
            // 复制图片
            if (function_exists("ImageCopyResampled"))
                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
            else
                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
            // 生成图片
            $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
            $imageFun($thumbImg, $thumbname);
            // 关闭画布
            imagedestroy($thumbImg);
            imagedestroy($srcImg);
            return $thumbname;
        }
        return false;
    }
}

你使用的时候直接用:

require 'Thumb.class.php';
$thumb = Thumb::create('s.jpg', 'thumb_s.jpg', 100, 50);

希望我的回答你能满意。

谁有php批量处理图片、图片生成缩略图、图片添加水印的函数?

// 批量处理图片、图片生成缩略图、图片添加水印
$dir = opendir(dirname(__FILE__));
while (!!$_file = readdir($dir)) {
    list($filesname, $kzm) = explode(".", $_file); // 获取扩展名
    if ($kzm == "gif" || $kzm == "jpg" || $kzm == "JPG" || $kzm == "png") {
        if (!makethumb($_file, "120", "120", "100")) {
            echo '执行成功!';
        } else {
            echo '执行失败!';
        }
    }
}
closedir($dir);
/**
 * 处理缩略图并添加水印函数
 * @access public
 * @param $srcFile-----------图片文件名
 * @param $dstFile-----------另存的文件名
 * @param $dstW-------------图片保存的宽度
 * @param $dstH--------------图片保存的高度
 * @param $rate---------------图片保存的品质
 * @param $markwords-----水印文字
 * @param $markimage-----水印图片
 * @param 使用方法 makethumb("a.jpg","b.jpg","120","120","100");
 */
function makethumb($srcFile /*,$dstFile*/ , $dstW, $dstH, $rate = 100 /*,$markwords=null,$markimage=null*/ ) {
    $data = GetImageSize($srcFile);
    switch ($data[2]) {
        case 1:
            $im = @ImageCreateFromGIF($srcFile);
            break;
        case 2:
            $im = @ImageCreateFromJPEG($srcFile);
            break;
        case 3:
            $im = @ImageCreateFromPNG($srcFile);
            break;
    }
    if (!$im) return False;
    $srcW = ImageSX($im);
    $srcH = ImageSY($im);
    $dstX = 0;
    $dstY = 0;
    if ($srcW * $dstH > $srcH * $dstW) {
        $fdstH = round($srcH * $dstW / $srcW);
        $dstY = floor(($dstH - $fdstH) / 2);
        $fdstW = $dstW;
    } else {
        $fdstW = round($srcW * $dstH / $srcH);
        $dstX = floor(($dstW - $fdstW) / 2);
        $fdstH = $dstH;
    }
    $ni = ImageCreateTrueColor($dstW, $dstH);
    $dstX = ($dstX < 0) ? 0 : $dstX;
    $dstY = ($dstX < 0) ? 0 : $dstY;
    $dstX = ($dstX > ($dstW / 2)) ? floor($dstW / 2) : $dstX;
    $dstY = ($dstY > ($dstH / 2)) ? floor($dstH / 2) : $dstY;
    $white = ImageColorAllocate($ni, 255, 255, 255);
    $black = ImageColorAllocate($ni, 0, 0, 0);
    imagefilledrectangle($ni, 0, 0, $dstW, $dstH, $white); // 填充背景色
    ImageCopyResized($ni, $im, $dstX, $dstY, 0, 0, $fdstW, $fdstH, $srcW, $srcH);
    $dstFile = $srcFile . '.gif';
    ImageJpeg($ni, $dstFile, $rate);
    imagedestroy($im);
    imagedestroy($ni);
}

php怎么生成缩略图

给你个函数吧:

// *****生成缩略图*****
// 只考虑jpg,png,gif格式
// $srcImgPath 源图象路径
// $targetImgPath 目标图象路径
// $targetW 目标图象宽度
// $targetH 目标图象高度
function makeThumbnail($srcImgPath, $targetImgPath, $targetW, $targetH) {
    $imgSize = GetImageSize($srcImgPath);
    $imgType = $imgSize[2];
    switch ($imgType) {
        case 1:
            $srcImg = @ImageCreateFromGIF($srcImgPath);
            break;
        case 2:
            $srcImg = @ImageCreateFromJpeg($srcImgPath);
            break;
        case 3:
            $srcImg = @ImageCreateFromPNG($srcImgPath);
            break;
    }
    // 取源图象的宽高
    $srcW = ImageSX($srcImg);
    $srcH = ImageSY($srcImg);
    if ($srcW < $targetW || $srcH < $targetH) {
        $targetX = 0;
        $targetY = 0;
        if ($srcW > $srcH) {
            $finaW = $targetW;
            $finalH = round($srcH * $finaW / $srcW);
            $targetY = floor(($targetH - $finalH) / 2);
        } else {
            $finalH = $targetH;
            $finaW = round($srcW * $finalH / $srcH);
            $targetX = floor(($targetW - $finaW) / 2);
        }
        if (function_exists("ImageCreateTrueColor")) {
            $targetImg = ImageCreateTrueColor($targetW, $targetH);
        } else {
            $targetImg = ImageCreate($targetW, $targetH);
        }
        $targetX = ($targetX < 0) ? 0 : $targetX;
        $targetY = ($targetX < 0) ? 0 : $targetY;
        $targetX = ($targetX > ($targetW / 2)) ? floor($targetW / 2) : $targetX;
        $targetY = ($targetY > ($targetH / 2)) ? floor($targetH / 2) : $targetY;
        // 背景白色
        $white = ImageColorAllocate($targetImg, 255, 255, 255);
        ImageFilledRectangle($targetImg, 0, 0, $targetW, $targetH, $white);
        if (function_exists("ImageCopyResampled")) {
            ImageCopyResampled($targetImg, $srcImg, $targetX, $targetY, 0, 0, $finaW, $finalH, $srcW, $srcH);
        } else {
            ImageCopyResized($targetImg, $srcImg, $targetX, $targetY, 0, 0, $finaW, $finalH, $srcW, $srcH);
        }
        switch ($imgType) {
            case 1:
                ImageGIF($targetImg, $targetImgPath);
                break;
            case 2:
                ImageJpeg($targetImg, $targetImgPath);
                break;
            case 3:
                ImagePNG($targetImg, $targetImgPath);
                break;
        }
        ImageDestroy($srcImg);
        ImageDestroy($targetImg);
    } else { // 不超出指定宽高则直接复制
        copy($srcImgPath, $targetImgPath);
        ImageDestroy($srcImg);
    }
}

代码已经测试,成功运行!