2016-09-28 78 views
-3

我需要在存储它们之前更改此代码以调整图像大小,超过500 kB。更改代码以在上传时调整图像大小

if(isset($_FILES['photo'])) 
    if(file_exists($_FILES['photo']['tmp_name']) || !is_uploaded_file($_FILES['photo']['tmp_name'])){ 
     $file_name = basename($_FILES['photo']['name']); 

     $temp = explode(".", $_FILES["photo"]["name"]); 
     $newfilename = round(microtime(true)) . '.' . end($temp); 
     $target_path = plugin_dir_path(__FILE__) . "uploads/$newfilename"; 

     if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) { 
      $handle = fopen($target_path, "rb"); 
      $fsize = filesize($target_path); 
      $img_contents = fread($handle, $fsize); 
      fclose($handle); 
     } 
    } 
+2

欢迎SO。 请阅读[我可以问哪些主题](http://stackoverflow.com/help/on-topic) 和[如何提出一个好问题](http://stackoverflow.com/help/how-to - 问) 和[完美的问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何创建[最小,完整和可验证的例子] (http://stackoverflow.com/help/mcve) SO是**不是一个免费的编码或代码转换或调试或教程或库查找服务** 您还必须表明,你已经努力解决你自己的问题。 – RiggsFolly

回答

1

这是您可能用于调整图像大小的功能之一:imagecopyresampled。我选择了这一个,因为它具有如何在用户贡献的笔记中维度属性工作的良好图表。

这是从该网页的代码,稍加修改实际上它保存为“simpleimage.jpg” ......

// The file 
$filename = 'test.jpg'; 
$percent = 0.5; 

// Get new dimensions 
list($width, $height) = getimagesize($filename); 
$new_width = $width * $percent; 
$new_height = $height * $percent; 

// Resample 
$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// Output 
imagejpeg($image_p, 'simpleimage.jpg', 100); 
+0

链接只有答案是皱眉。添加至少一些有用的描述作为关闭站点资源的链接通常会被破坏 – RiggsFolly

+0

已被编辑。我主要想给出PHP函数的名字,但你是对的。 – sdexp

+0

这比紫外线更好 – RiggsFolly

相关问题