首页 > 解决方案 > 压缩图像的问题

问题描述

我想压缩和下载图像。

为此,我尝试了一些代码,但图像下载正确但没有压缩。

它向我显示了一个错误PHP Warning: imagejpeg(email.png): failed to open stream: Permission denied

我把我的代码放在下面。

<div class="message">
<?php
   if($_POST){
    	if ($error) {
    	?>
           <label class="error"><?php echo $error; ?></label>
        <?php
        }
   }
?>
</div>
<fieldset class="well">
    <legend>Upload Image:</legend>
    <form action="" name="myform" id="myform" method="post" enctype="multipart/form-data">
        <ul>
            <li>
                <label>Upload:</label>
                <input type="file" name="file" id="file"/>
            </li>
            <li>
                <input type="submit" name="submit" id="submit" class="submit btn-success"/>
            </li>
        </ul>
    </form>
</fieldset>

    $name  = '';
$type  = '';
$size  = '';
$error = '';
function compress_image($source_url, $destination_url, $quality)
{
    $info = getimagesize($source_url);
    if ($info['mime'] == 'image/jpeg')
        $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source_url);
    imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}
if ($_POST) {
    if ($_FILES["file"]["error"] > 0) {
        $error = $_FILES["file"]["error"];
    } 
    else if (($_FILES["file"]["type"] == "image/gif") || 
    ($_FILES["file"]["type"] == "image/jpeg") || 
    ($_FILES["file"]["type"] == "image/png") || 
    ($_FILES["file"]["type"] == "image/pjpeg")) {

    $url = $_FILES["file"]["name"];

    $filename = compress_image($temp_file, $url, 80);
    rename($filename, 'image/'.$filename);
    $location = "image/".$url;
    if (file_exists($location))
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($location));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($location));
        ob_clean();
        flush();
        readfile($location);
        exit;
    }
    }else {
        $error = "Uploaded image should be jpg or gif or png";
    }
}

使用此代码,图像已正确下载,但未压缩。

我不知道为什么会这样。

谁能帮我解决这个问题

标签: phphtml

解决方案


这是您需要在代码中更新的更新代码。请仅更新这部分代码

$filename = compress_image($temp_file, $url, 80);
rename($filename, 'image/'.$filename);
$location = "image/".$url;
if (file_exists($location))
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($location));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($location));
    ob_clean();
    flush();
    readfile($location);
    exit;
}

function compress_image($source_url, $destination_url, $quality)
{
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg'){
     $image = imagecreatefromjpeg($source_url);
    imagejpeg($image, $destination_url, $quality);
}

elseif ($info['mime'] == 'image/gif'){
     $image = imagecreatefromgif($source_url);
     imagegif($image, $destination_url, $quality);
}

elseif ($info['mime'] == 'image/png'){
    $image = imagecreatefrompng($source_url);
    imagepng($image, $destination_url,5);
}


return $destination_url;
}

推荐阅读