压缩图片的问题 [英] issue with compressing images

查看:26
本文介绍了压缩图片的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想压缩和下载图片.

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

它向我显示了 PHP 错误警告:imagejpeg(email.png):无法打开流:权限被拒绝

我已经把我的代码放在下面了.

<?php如果($_POST){如果($错误){?><label class="error"><?php echo $error;?></label><?php}}?>

<fieldset class="well"><legend>上传图片:</legend><form action="" name="myform" id="myform" method="post" enctype="multipart/form-data"><ul><li><label>上传:</label><input type="file" name="file" id="file"/><li><input type="submit" name="submit" id="submit" class="submit btn-success"/></表单></fieldset>

 $name = '';$type = '';$size = '';$错误 = '';功能 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);返回 $destination_url;}如果($_POST){如果 ($_FILES["文件"]["错误"] > 0) {$error = $_FILES["文件"]["错误"];}else if (($_FILES["file"]["type"] == "image/gif") ||($_FILES["file"]["type"] == "image/jpeg") ||($_FILES["file"]["type"] == "image/png") ||($_FILES["file"]["type"] == "image/pjpeg")) {$url = $_FILES["文件"]["名称"];$filename = compress_image($temp_file, $url, 80);重命名($文件名,'图像/'.$文件名);$location = "图片/".$url;如果(文件存在($位置)){header('内容描述:文件传输');header('Content-Type: application/octet-stream');header('Content-Disposition: 附件;文件名='.basename($location));header('过期时间:0');header('缓存控制:必须重新验证');header('Pragma: public');header('Content-Length:' .filesize($location));ob_clean();冲洗();读取文件($位置);出口;}}别的 {$error = "上传的图片应该是 jpg 或 gif 或 png";}}

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

我不知道为什么会这样.

谁能帮我解决这个问题

解决方案

这里是你需要在代码中更新的更新代码.请只更新这部分代码

$filename = compress_image($temp_file, $url, 80);重命名($文件名,'图像/'.$文件名);$location = "图片/".$url;如果(文件存在($位置)){header('内容描述:文件传输');header('Content-Type: application/octet-stream');header('Content-Disposition: 附件;文件名='.basename($location));header('过期时间:0');header('缓存控制:必须重新验证');header('Pragma: public');header('Content-Length:' .filesize($location));ob_clean();冲洗();读取文件($位置);出口;}功能 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);}返回 $destination_url;}

I want to compress and download images.

for that, I have tried some code but with that Images downloaded properly but it is not compressing.

it shows me an error with PHP Warning: imagejpeg(email.png): failed to open stream: Permission denied

I have put my code below.

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

with this code, the image is downloaded properly but it is not compressing.

I don't know why is this happening.

can anybody help me with this

解决方案

here is the updated code that you need to update in your code.please update only this part of code

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

这篇关于压缩图片的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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