How to move compressed image file PHP

我只是一个虾纸丫 提交于 2019-12-13 08:05:38

问题


Here is the function that compress image file.

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

$filename = compress_image($_FILES["home_image"]["tmp_name"], $_FILES["home_image"]["name"], 80); 
$buffer = file_get_contents($_FILES["home_image"]["name"]);

Here is my code that want to move the compressed image to my specific folder

move_uploaded_file($_FILES["home_image"]["tmp_name"][$i],"../../../Test/image/home_banner/" .$filename);

But the image that move to the folder is still remain the original size without compress.

Am I doing the mistake..?


回答1:


I think move_uplodaded_file() is not appropriate here, because you change the uploaded file contents:

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

Use rename() instead.

I'm also not 100% if you can edit the uploaded file directly..



来源:https://stackoverflow.com/questions/24476578/how-to-move-compressed-image-file-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!