Resizing images - Errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DAVID87
Forum Commoner
Posts: 29
Joined: Fri Jun 26, 2009 9:56 am
Location: Nottingham, UK

Resizing images - Errors

Post by DAVID87 »

Hi All,

I have the below code that takes an image from an input form (user browses for image). Once the user clicks add the form data is handed over to a script which processes a number of things including resizing images and saving them on the server.

This is not an open code and is for admin only and is password protected to view the page.

the code for resizing and saving the images are below. On the form a user can put in 5 images named "new_image", "new_image1", "new_image2", "new_image3" and "new_image_main". The "new_image_main" is handled slightly differently as only 1 size is needed. This code below is coppied over 4 times changing the file input names and the output names.

The issue that I am having now is that if the file input field is left blank (i.e. no image is selected) then the whole form is processed fine just errors are shown to the user saying that files cannot be found and then other errors because of the file not being found. Can someone please advise where This script is going wrong.

Basically if there is no picture inserted (input file) then it skips to the next image.

Code: Select all

<?php
// 3rd image
    if (isset ($_FILES['new_image2'])){
        $imagename = $_POST['title'];
        $source = $_FILES['new_image2']['tmp_name'];
        $target = "assets/car_pics/2_".$imagename;
        move_uploaded_file($source, $target);
        
        $imagepath = $imagename;
        $save = "assets/car_pics/2_" . $imagepath; //This is the new file you saving
        $file = "assets/car_pics/2_" . $imagepath; //This is the original file
        
        list($width, $height) = getimagesize($file) ; 
        
        $modwidth = 1600; 
        
        $diff = $width / $modwidth;
        
        $modheight = $height / $diff;
        $tn = imagecreatetruecolor($modwidth, $modheight) ;
        $image = imagecreatefromjpeg($file) ;
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
        
        imagejpeg($tn, $save, 100) ;
        
        $save = "assets/car_pics/l_2_" . $imagepath; //This is the new file you saving
        $file = "assets/car_pics/2_" . $imagepath; //This is the original file
        
        list($width, $height) = getimagesize($file) ; 
        $modwidth = 1200; 
        
        $diff = $width / $modwidth;
        
        $modheight = $height / $diff; 
        $tn = imagecreatetruecolor($modwidth, $modheight) ; 
        $image = imagecreatefromjpeg($file) ; 
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
        
        imagejpeg($tn, $save, 100) ;
        rename('assets/car_pics/l_2_'.$imagepath, 'assets/car_pics/'.$imagepath.'_3.jpg');
        }
        
        if (isset ($_FILES['new_image2'])){
        
        $imagepath = $imagename;
        $save = "assets/car_pics/2_" . $imagepath; //This is the new file you saving
        $file = "assets/car_pics/2_" . $imagepath; //This is the original file
        
        list($width, $height) = getimagesize($file) ; 
        
        $modwidth = 100; 
        
        $diff = $width / $modwidth;
        
        $modheight = $height / $diff; 
        $tn = imagecreatetruecolor($modwidth, $modheight) ; 
        $image = imagecreatefromjpeg($file) ; 
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
        
        imagejpeg($tn, $save, 100) ; 
 
        $save = "assets/car_pics/s_2_" . $imagepath; //This is the new file you saving
        $file = "assets/car_pics/2_" . $imagepath; //This is the original file
        
        list($width, $height) = getimagesize($file) ; 
        
        $modwidth = 75; 
 
        $diff = $width / $modwidth;
        
        $modheight = $height / $diff; 
        $tn = imagecreatetruecolor($modwidth, $modheight) ; 
        $image = imagecreatefromjpeg($file) ; 
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
        
        imagejpeg($tn, $save, 100) ; 
        
        rename('assets/car_pics/s_2_'.$imagepath, 'assets/car_pics/s_'.$imagepath.'_3.jpg');
        rename('assets/car_pics/2_'.$imagepath, 'assets/car_pics/delete_me_'.$imagepath.'_3.jpg'); 
 
mysql_query("UPDATE test SET img_3='$_POST[title]' WHERE title='$_POST[title]'") or die(mysql_error());
 
$tmpfile3 = "assets/car_pics/delete_me_".$imagepath."_3.jpg";
unlink($tmpfile3);
}
?>
I know this code is probably abit messy but its how I was told to do it and it seems to work but it just shows errors.

Any Help?
Post Reply