I have an uploading system on my website which detects whether an image is larger than a certain size and if so resizes it to be that size, using the code below:
<?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>";
}
}
?>
However one the following errors or problems occur:
-
I get the following errors:
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 220Warning: unlink(DSC00103.JPG) [function.unlink]: No such file or directory in /homepages/19/d372249701/htdocs/ap-photo/admin/new_photo.php on line 221
-
Or it just does not transfer the image
Both of these outcomes suggest that the image that I am uploading does not exist?!?
Any suggestions for a solution would be gratefully received.
UPDATE 1
The .htaccess file I use to protect files for copyright purposes:
RewriteEngine On
RewriteCond %{REQUEST_URI} $
RewriteRule \.(gif|jpg|png|JPG)$ - [L,F]
ErrorDocument 403 /admin/uploads/error-docs/403.shtml
UPDATE 2
I have gone through the code and identified that the problem is between:
exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image");
and
$copy = copy($new_image, "/homepages/19/d372249701/htdocs/ap-photo/admin/uploads/images/photography/".$new_image);
$unlink = unlink($new_image);
I doubt it is the echos.
I would try:
1/ displaying any errors during upload with the php errors: http://php.net/manual/en/features.file-upload.errors.php
2/ Display the variables with echo at the different stages to see what they contain.
e.g.
3/ Isolate the individual parts of the code and check each step works.
Editted answer:
To be honist I did not read the Imagemagick code!
I write my code like this which makes it a bit easier to error check the Imagemagick code as you can add an echo to show the contents of $cmd
Imagemagick is not great for error checking but you can add this:
$new_image just needs to be the relative path to the save location with the image name.