Image Upload overwrite
Posted: Wed Mar 29, 2006 5:46 pm
The script below worked fine for uploading and resizing images and creating a large image and a thumbnail based on dimensions. The images are uploaded to the "images" folder which has a chmod of 777 on it. But for soem reason it is not able to overwrite the exisiting image of the same name and spits out these errors. This is really frustrating. Any help will be appreciated. Thanks
The code is as under
Code: Select all
Warning: imagejpeg(): Unable to open 'images/S1005G.jpg' for writing in /home/soderbrg/public_html/admin.php on line 1819
Warning: imagejpeg(): Unable to open 'images/S1005G_th.jpg' for writing in /home/soderbrg/public_html/admin.php on line 1840Code: Select all
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////// - Image Upload and Resize Function - ////////////////////////
if ($action=='uploadimg') {
if (session_is_registered("uid")) {
$prod_id=$_GET['prod_id'];
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];
$split = explode(".", $filename);
$name=$split[0];
switch($mimetype) {
case "image/jpg":
case "image/jpeg":
case "image/pjpeg":
$i = imagecreatefromjpeg($temporary_name);
break;
case "image/gif":
$i = imagecreatefromgif($temporary_name);
break;
case "image/png":
$i = imagecreatefrompng($temporary_name);
break;
}
unlink($temporary_name);
//imagejpeg($i,"images/uploadedfile.jpg",80);
//Specify the size of the new product image
$dest_x = 300;
$dest_y = 300;
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
if (imagesx($i) >= imagesy($i)) {
$thumb_x = $dest_x;
$thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$thumb_x = imagesx($i)*($dest_y/imagesy($i));
$thumb_y = $dest_y;
}
} else {
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
//Save the product image
imagejpeg($thumb, "images/".$name.".jpg", 80);
//Creating the thumbnail image
$dest_x = 75;
$dest_y = 75;
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
if (imagesx($i) >= imagesy($i)) {
$thumb_x = $dest_x;
$thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$thumb_x = imagesx($i)*($dest_y/imagesy($i));
$thumb_y = $dest_y;
}
} else {
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
imagejpeg($thumb, "images/".$name."_th.jpg", 80);
$sql="update product_pages set prod_image='$filename', prod_image_th='".$name."_th.jpg' where prod_id='".$prod_id."'";
$result = mysql_query($sql);
?>
<tr>
<td class="text" width="100%" align="center">The Image : <b><?=$filename?></b> has been added for the Product.</td>
</tr>
<tr>
<td class='text' align='center'><a href="admin.php?action=uploadfile&prod_id=<?=$prod_id?>" class="link">Click here to Upload a Document</a> | <a href="admin.php?action=admin" class="link">Admin Main Page</a></td>
</tr>
<?
} else {
?>
<script LANGUAGE="JavaScript">
<!--
location='admin.php?action=login ';
// -->
</script>
<?
}
}