我如何在PHP中压缩图像文件并存储在文件夹中

时间:2017-01-20 07:05:20

标签: php html

在数组中$_FILES一切都很完美。我该如何压缩文件?变量$Buffer以base64格式返回。

这是我的代码:

$url = 'destination1.jpg'; 
$filename = compress_image($_FILES["uploadfile"]["tmp_name"], $url, 80); 
$buffer = file_get_contents($url); /* Force download dialog... */ 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); /* Don't allow caching... */ 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); /* Set data type, size and filename */ 
header("Content-Type: application/octet-stream"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . strlen($buffer)); 
header("Content-Disposition: attachment; filename=$url"); /* Send our file... */ 
$buffer; 

compress_image函数如下:

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

   imagejpeg($image, $destination_url, $quality); 
   // echo $destination_url;exit();
   return $destination_url; 
} 

请帮帮我。

0 个答案:

没有答案