如何在PHP中将所有图像转换为JPG格式? [英] How to convert all images to JPG format in PHP?

查看:121
本文介绍了如何在PHP中将所有图像转换为JPG格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP开发一个网站,让用户上传图像然后让他决定图像应该如何使用jQuery - PHP整合来选择想要成为图片的区域,然后点击裁剪按钮裁剪它并保存它。

I am developing a website in PHP that let the user to upload images and then let him to decide how the image should be using jQuery - PHP integeration to select the area that wanted to be the picture and then click the crop button to crop it and save it.

我面临的问题是并非所有图像类型都能很好地裁剪和保存所以我注意到它的简单解决方案图像到JPG,然后让用户裁剪它,因为这是以JPG格式进行的简单方法。

The problem that I am facing is that not all images type are good to crop and save so I noticed that the easy solution for it to convert the image to JPG and then let the user to crop it because it's the easy way to do it in JPG format.

我怎么做?

这是图像类型问题的最佳解决方案吗?

Is this the best solution for images types problem?

编辑:

我正在使用此代码裁剪图像,它不是以PNG格式拍摄,也不限于3分机。

I am using this code to crop images and it's not wroking in PNG format and also limited to 3 ext.

$path_parts = pathinfo("../images/DVDs/".$_POST['logo_file']);
        if ($path_parts['extension'] == "png") {

                $src = imagecreatefrompng("../images/DVDs/".$_POST['logo_file']);

                        $tmp = imagecreatetruecolor(350, 494);
                        imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],350,494,$_POST['w'],$_POST['h']);
                        imagepng($tmp, "../images/DVDs/$filename".'t_'.$_POST['logo_file'],100);
            } else if ($path_parts['extension'] == "jpg" || $path_parts['extension'] == "jpeg") {

                        $src = imagecreatefromjpeg("../images/DVDs/".$_POST['logo_file']);

                        $tmp = imagecreatetruecolor(350, 494);
                        imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],350,494,$_POST['w'],$_POST['h']);
                        imagejpeg($tmp, "../images/DVDs/$filename".'t_'.$_POST['logo_file'],100);
                } else if ($path_parts['extension'] == "gif") {

                        $src = imagecreatefromgif("../images/DVDs/".$_POST['logo_file']);

                        $tmp = imagecreatetruecolor(350, 494);
                        imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],350,494,$_POST['w'],$_POST['h']);
                        imagegif($tmp, "../images/DVDs/$filename".'t_'.$_POST['logo_file'],100);
                    }

我想将图像转换为JPG格式,因为它是最容易转换的问题。

I want to convert images to JPG format because it's the easiest to convert without any problem.

推荐答案

也许它不能用于PNG,因为PNG只支持0到9的压缩等级。

Maybe it's not working with PNG because PNG only supports compression levels 0 to 9.

我还宁愿根据MIME类型而不是扩展名修改行为。我想你在代码中使用它之前检查你的POST用户输入;)

I'd also rather modify the behaviour based on MIME type, not extension. And I guess you're checking your POST user input before using it in code ;)

这是我的代码变体:

$path = "../images/DVDs/";

$img = $path . $_POST['logo_file'];

if (($img_info = getimagesize($img)) === FALSE)
  die("Image not found or not an image");


switch ($img_info[2]) {
  case IMAGETYPE_GIF  : $src = imagecreatefromgif($img);  break;
  case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
  case IMAGETYPE_PNG  : $src = imagecreatefrompng($img);  break;
  default : die("Unknown filetype");
}

$tmp = imagecreatetruecolor(350, 494);
imagecopyresampled($tmp, $src, 0, 0, intval($_POST['x']), intval($_POST['y']),
                   350, 494, intval($_POST['w']), intval($_POST['h']));


$thumb = $path . pathinfo($img, PATHINFO_FILENAME) . "_thumb";
switch ($img_info[2]) {
  case IMAGETYPE_GIF  : imagegif($tmp,  $thumb . '.gif');      break;
  case IMAGETYPE_JPEG : imagejpeg($tmp, $thumb . '.jpg', 100); break;
  case IMAGETYPE_PNG  : imagepng($tmp,  $thumb . '.png', 9);   break;
  default : die("Unknown filetype");
}

对于您想要支持的每种文件类型,您只需添加两行代码。

For every filetype you want supported, you only have to add two lines of code.

这篇关于如何在PHP中将所有图像转换为JPG格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆