php – 如何将所有图像转换为jpg?

前端之家收集整理的这篇文章主要介绍了php – 如何将所有图像转换为jpg?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有脚本:
<?PHP

include('db.PHP');
session_start();
$session_id = '1'; // User session id
$path = "uploads/";

$valid_formats = array("jpg","png","gif","bmp","jpeg");
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];
    if (strlen($name)) {
        list($txt,$ext) = explode(".",$name);
        if (in_array($ext,$valid_formats)) {
            if ($size < (1024 * 1024)) { // Image size max 1 MB
                $actual_image_name = time() . $session_id . "." . $ext;
                $tmp = $_FILES['photoimg']['tmp_name'];
                if (move_uploaded_file($tmp,$path . $actual_image_name)) {
                    MysqL_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
                    echo "<img src='uploads/" . $actual_image_name . "' class='preview'>";
                } else {
                    echo "Failed";
                }
            } else {
                echo "Image file size max 1 MB";
            }
        } else {
            echo "Invalid file format..";
        }
    } else {
        echo "Please select image..!";
    }
    exit;
}

?>

可以将所有图像(png,gif等)转换为100%质量的jpg吗?如果有,怎么样?我想允许上传png和gif,但是这个脚本应该将这些文件转换为jpg.有可能这与PHP

试试这段代码:originalImage是…原始图像的路径… outputImage足够自我解释.质量是从0到100的数字,设置输出jpg质量(0 – 最差,100 – 最好)
function convertImage($originalImage,$outputImage,$quality)
{
    // jpg,png,gif or bmp?
    $exploded = explode('.',$originalImage);
    $ext = $exploded[count($exploded) - 1]; 

    if (preg_match('/jpg|jpeg/i',$ext))
        $imageTmp=imagecreatefromjpeg($originalImage);
    else if (preg_match('/png/i',$ext))
        $imageTmp=imagecreatefrompng($originalImage);
    else if (preg_match('/gif/i',$ext))
        $imageTmp=imagecreatefromgif($originalImage);
    else if (preg_match('/bmp/i',$ext))
        $imageTmp=imagecreatefrombmp($originalImage);
    else
        return 0;

    // quality is a value from 0 (worst) to 100 (best)
    imagejpeg($imageTmp,$quality);
    imagedestroy($imageTmp);

    return 1;
}

猜你在找的PHP相关文章