我有一个水印脚本,你们之前可能已经看过我之前的一个问题:
<?php
$imagesource = $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (!$image) die();
$watermark = @imagecreatefrompng('watermark_'.(imagesx($image) <= 1100 ? "port" : "lans").'.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth,
$watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
脚本工作正常,但是当我的客户端上传超过2000px X 2000px的图片(或者周围的东西)时,脚本甚至不输出任何错误。但是,当我将其减少到1100px X 800px时,例如此脚本输出并显示图像。这有什么理由吗?
无论如何要么诊断导致这种情况的原因并因此找到解决方案或者在上传时自动重新调整图像大小?
这是我目前的上传脚本:
<?php
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/" . $_FILES["file"]["name"]))
{
echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "<h3>Invalid file</h3>";
}
?>
答案 0 :(得分:1)
无论如何要么诊断导致这种情况的原因并找到解决方案
正如nickb建议的那样,检查服务器的error_log
。根据您配置PHP的方式,这可能在Apache的error_log
中,它可能位于单独的文件中,或者可能不存在,需要打开。
有关详细信息,请参阅PHP manual's section on error handling,特别是the php.ini
error_log
directive。
我怀疑你的脚本可能内存不足。你的内存限制是多少? (<?php phpinfo();
可以告诉你这个)。您是否尝试在php.ini
中增加它?
或[有什么办法]在上传时自动重新调整图片大小?
是。有lots of ways to upload an image and resize {{3} }
你试过it吗? :-)
答案 1 :(得分:1)
感谢Josh,我找到了这个我已经整合的直接解决方案。再次感谢!
<?php
if($_REQUEST['post']==1){
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/" . $_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 );
// 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=\"'uploads/".$new_image."\" width=\"900\"><br><br>";
$copy = copy($new_image, "uploads/".$new_image);
$delete = unlink($new_image);
if($copy){
echo "Stored in: " . "uploads/" . $_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"].'")');
$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"].'" , "6.00" , \'6" x 4"\')');
$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"].'" , "8.00" , \'5" x 7"\')');
$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"].'" , "10.00" , \'8" x 6"\')');
$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"].'" , "12.00" , \'10" x 8"\')');
$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"].'" , "15.00" , \'12" x 8"\')');
}else{
echo "Unable to copy file";
}
}
}
}
else
{
echo "<h3>Invalid file</h3>";
}
?>
<br />
<br />
<h3>
<?php
if($query){
echo "Successful";
}else{
echo "Unsuccessful";
}
}
?>