Jump to content

Miss out image manupulation if no image is sent


petenaylor

Recommended Posts

Hi all

 

I am in the middle of a script that enables the user to upload an image via a form and then it changes the size and uploads it to the server and to a mySQL database. The user has the ability to upload three images.

 

I need to know how to bypass part of the script for the images that they don't upload. For example of they only upload one via the form how do I miss out the section of the script that does the other two images?

 

Here's my code:

 

$left_title = stripslashes($_POST['left_title']);

$left_text = stripslashes($_POST['left_text']);

 

$image1 = $_FILES['image1']['tmp_name'];

$image1main = $_FILES['image1']['name']; //image 1

$src = imagecreatefromjpeg($image1);

 

$image2 = $_FILES['image2']['tmp_name'];

$image2main = $_FILES['image2']['name']; // image 2

$src2 = imagecreatefromjpeg($image2);

 

$image3 = $_FILES['image3']['tmp_name'];

$image3main = $_FILES['image3']['name']; // image 3

$src3 = imagecreatefromjpeg($image3);

 

//image number 1 creation and change ---------------------------------------------------------------------------------------------

list($width,$height)=getimagesize($image1);

$newwidth=600;

$newheight=($height/$width)*$newwidth;

$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = "/home/users/web/b2075/moo.petenaylor/websites/d/dogs/images/front/". $_FILES['image1']['name'];

imagejpeg($tmp,$filename,100);

imagedestroy($src);

imagedestroy($tmp);

//end of image 1 manipulation ----------------------------------------------------------------------------------------------------

 

//image number 2 creation and change ---------------------------------------------------------------------------------------------

list($width,$height)=getimagesize($image2);

$newwidth=600;

$newheight=($height/$width)*$newwidth;

$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src2,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = "/home/users/web/b2075/moo.petenaylor/websites/d/dogs/images/front/". $_FILES['image2']['name'];

imagejpeg($tmp,$filename,100);

imagedestroy($src2);

imagedestroy($tmp);

//end of image 2 manipulation ----------------------------------------------------------------------------------------------------

 

//image number 3 creation and change ---------------------------------------------------------------------------------------------

list($width,$height)=getimagesize($image3);

$newwidth=600;

$newheight=($height/$width)*$newwidth;

$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src3,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = "/home/users/web/b2075/moo.petenaylor/websites/d/dogs/images/front/". $_FILES['image3']['name'];

imagejpeg($tmp,$filename,100);

imagedestroy($src3);

imagedestroy($tmp);

//end of image 3 manipulation ----------------------------------------------------------------------------------------------------

 

 

// Validation

//saving record to MySQL database

 

//(\"$item_category\",\"$title\",\"$description\",\"$price\",\"$postage\",\"$condition\",\"$item_code\",\"$image1main\",\"$image2main\",\"$image3main\",\"$image4main\")" ;

 

@$pfw_strQuery = "INSERT INTO `front_page_text`(`left_title`,`left_text`,`image1`,`image2`,`image3`)VALUES (\"$left_title\",\"$left_text\",\"$image1main\",\"$image2main\",\"$image3main\")" ;

 

 

 

Many thanks for you help.

Pete

Link to comment
Share on other sites

The better way to do that would be:

<?php

$left_title = stripslashes($_POST['left_title']);
$left_text = stripslashes($_POST['left_text']);

$images[1] = $_FILES['image1']['tmp_name'];
$image1main = $_FILES['image1']['name']; //image 1
$src = imagecreatefromjpeg($images[1]);

$images[2] = $_FILES['image2']['tmp_name'];
$image2main = $_FILES['image2']['name']; // image 2
$src2 = imagecreatefromjpeg($images[2]);

$images[3] = $_FILES['image3']['tmp_name'];
$image3main = $_FILES['image3']['name']; // image 3
$src3 = imagecreatefromjpeg($images[3]);


for ($n = 1; $n <= $number_of_images_uploaded; $n++)
{
list($width,$height)=getimagesize($images[$n]);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "/home/users/web/b2075/moo.petenaylor/websites/d/dogs/images/front/". $_FILES['image'.$n]['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}


// Validation
//saving record to MySQL database

//(\"$item_category\",\"$title\",\"$description\",\"$price\",\"$postage\",\"$condition\",\"$item_code\",\"$image1main\",\"$image2main\",\"$image3main\",\"$image4main\")" ;

@$pfw_strQuery = "INSERT INTO `front_page_text`(`left_title`,`left_text`,`image1`,`image2`,`image3`)VALUES (\"$left_title\",\"$left_text\",\"$image1main\",\"$image2main\",\"$image3main\")" ;
?>

 

That should work. assuming $number_of_images_uploaded contains (surprise surprise!) the number of images that were uploaded.

Also I replaced $image1, $image2 and $image3 with one array $images.

Link to comment
Share on other sites

Hi there

 

That seems to work, but the images aren't being uploaded to the server? The correct image names are being added to the database and they display on the page but the image isn't in the directory.

 

I have made sure the CHMOD is OK on the directory too.

 

 

Link to comment
Share on other sites

The actual images show but the file not being on the server means that the placeholder is there but no image shows. So the data is in the database correctly and the page is showing the correct image, but the image isn't physically in the directory on the server so it's not shown.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.