图像调整大小上传imagemagick

时间:2012-05-17 14:46:25

标签: php .htaccess image-processing imagemagick

我的网站上有一个上传系统,用于检测图片是否大于特定尺寸,如果是,则使用以下代码调整大小:

<?php
if($count >= "1"){
    echo "<h3>The Name : ".$_POST['textfield']. " already exists. </h3>";
    echo "<h3>Please choose a unique name for this photo</h3>";
}else{  
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";



    if (file_exists("uploads/images/photography/" . $_FILES["file"]["name"]))
      {
      echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
      }
    else
      {

// Temporary upload image name 
$original_image = $_FILES["file"]["tmp_name"]; 

// Get the image dimensions 
$size=getimagesize( $original_image ); 

if((($size[0])<1600) || (($size[1])<1600)){

$new_image = $_FILES["file"]["name"]; 

move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/images/photography/" . $_FILES["file"]["name"]);


echo "File uploaded<br><br>"; 

echo "<img src=\"watermark.php?path=".$new_image."\" width=\"900\"><br><br>"; 

}else{

// Name to save the image as - in this case the same as the original 
$new_image = $_FILES["file"]["name"]; 

// Maximum image width 
$max_width = "1600"; 

// Maximum image height 
$max_height = "1600"; 

// Resize the image and save 
exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image"); 

echo "File uploaded<br><br>"; 

echo "<img src=\"watermark.php?path=".$new_image."\" width=\"900\"><br><br>"; 

$copy = copy($new_image, "uploads/images/photography/".$new_image);
$unlink = unlink($new_image);

}

      echo "Stored in: " . "uploads/images/photography/" . $_FILES["file"]["name"];

      $date = date("d/m/y");

      $query  = mysql_query ('INSERT INTO  `ap_photos_list` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'")');

      $sizes = mysql_query("SELECT * FROM ap_sizes ORDER BY id ASC");

      while($row = mysql_fetch_assoc($sizes)){
            $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "'.$row['price'].'" , \''.$row['size'].'\')');
      }
      }
    }
  }
else
  {
  echo "<h3>Invalid file</h3>";
  }
}
?>

然而,出现以下错误或问题:

  • 我收到以下错误:

    1. Warning: copy(DSC00103.JPG) [function.copy]: failed to open stream: No such file or directory in /homepages/19/d372249701/htdocs/ap-photo/admin/new_photo.php on line 220
    2. Warning: unlink(DSC00103.JPG) [function.unlink]: No such file or directory in /homepages/19/d372249701/htdocs/ap-photo/admin/new_photo.php on line 221
  • 或者它只是不传输图像

这两个结果都表明我上传的图片不存在?!?

我们非常感激地提出任何解决方案的建议。

更新1

我用来保护文件的.htaccess文件用于版权目的:

RewriteEngine On
RewriteCond %{REQUEST_URI} $
RewriteRule \.(gif|jpg|png|JPG)$ - [L,F]
ErrorDocument 403 /admin/uploads/error-docs/403.shtml

更新2

我已经完成了代码并确定问题出在:

之间
  • exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image");

  • $copy = copy($new_image, "/homepages/19/d372249701/htdocs/ap-photo/admin/uploads/images/photography/".$new_image); $unlink = unlink($new_image);

我怀疑这是回声。

3 个答案:

答案 0 :(得分:0)

要检查的几件事情:

  • 您尝试保存的目录是否可写? (上传/图像/摄影)
  • 图像是否大于PHP设置允许的范围? (通常在php.ini中,upload_max_filesize)

修改

您需要在“复制”脚本中找到上传位置的绝对路径,应该

开头
/homepages/19/d372249701/htdocs

答案 1 :(得分:0)

似乎您的upload_max_filesize设置为默认的2MB。请使用以下内容设置.htaccess:

php_value upload_max_filesize    110M
php_value post_max_size          120M

有关详细信息,请参阅here

答案 2 :(得分:0)

我会尝试:

1 /在上传期间显示任何错误,并显示错误:http://php.net/manual/en/features.file-upload.errors.php

2 /在不同阶段显示带有回声的变量,以查看它们包含的内容。

e.g。

$original_image = $_FILES["file"]["tmp_name"];
echo "original image = $original_image<br>";

3 /隔离代码的各个部分,并检查每个步骤是否有效。

编辑答案:

要成为荣誉,我没有阅读Imagemagick代码!

-size {$size[0]}x{$size[1]} is not required

我编写了这样的代码,这使得更容易错误检查Imagemagick代码,因为你可以添加一个echo来显示$ cmd的内容

$cmd = "$original_image -thumbnail $max_widthx$max_height ";
exec(" convert $cmd $new_image"); 

Imagemagick不适合错误检查,但您可以添加:

$array=array(); 
echo "<pre>"; 
$cmd = "$original_image -thumbnail $max_widthx$max_height ";
exec(" convert $cmd $new_image 2>&1", $array);  
echo "<br>".print_r($array)."<br></pre>";

$ new_image只需要是带有图像名称的保存位置的相对路径。