压缩图像的问题

时间:2019-12-19 10:55:12

标签: php html

我想压缩并下载图像。

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

它显示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";
    }
}

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

我不知道为什么会这样。

有人可以帮我吗

1 个答案:

答案 0 :(得分:0)

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

$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;
}