Problem uploading and resizing multiple images

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
SabirAhmed
Forum Newbie
Posts: 23
Joined: Tue Aug 02, 2011 11:56 am

Problem uploading and resizing multiple images

Post by SabirAhmed »

Hi guys, I'm really stumped on this point and i hope someone can help me.

Basically I have set up some php coding to allow the user to upload pictures, up to 10, and depending on the size of them the PHP should make copies of the image and resize it.

I've set up the form and the PHP coding to actually resize the images is performed by an include.

The problem I am getting is that when I try and upoload a picture which is 'large' (more than 1500 pixels height), it will upload the picture, but it will not upload the next picture if the next picture is a medium - less than 1500 height).

If I upload all pictures which are medium in size then it works fine ... it is just when I include a large picture that some medium-sized subsequent pictures do not get uploaded.

Code: Select all


[syntax=php]<?php
$newimage = "";
$newtarget = ""; # from now TARGET_PATH1 is not going to change with alterations, it needs to be specified at the end that $TARGET_PATH1_MED is $NEWTARGETPATH_MED etc before this page is finished to allow for placing of all files together at end

$newfile_type = "";
$new_image_ = "";
$new_height = "";
$new_width = "";
$newnew_image_med = "";
$newnew_image_thumb = "";
$newnew_image_large = "";
$newresized_image_med = "";
$newresized_image_thumb = "";
$newresized_image_large = "";

$NEWTARGETPATH_MED = "";
$NEWTARGETPATH_THUMB = "";
$NEWTARGETPATH_LARGE = "";

$newfile_type = "";

$newimage = $image4;
$newtarget = $TARGET_PATH4; # from now TARGET_PATH1 is not going to change with alterations, it needs to be specified at the end that $TARGET_PATH1_MED is $NEWTARGETPATH_MED etc before this page is finished to allow for placing of all files together at end

$newfile_type = $file_type4;
$new_image_ = $new_img4;
$new_height = $height4;
$new_width = $width4;
$newnew_image_med = $new_img4_med;
$newnew_image_thumb = $new_img4_thumb;
$newresized_image_med = $resized_img4_med;
$newresized_image_thumb = $resized_img4_thumb;


$NEWTARGETPATH_MED = $TARGET_PATH4_med;
$NEWTARGETPATH_THUMB = $TARGET_PATH4_thumb;

$newfile_type = $_FILES['image4']['type'];



$Upperheight = 1500;
$Upperwidth = 2000;
$Med_height = 200;
$Med_width = 533;
$large_height = 1500;
$large_width = 2000;

// Build our target path full string. This is where the file will be moved do
// i.e. images/picture.jpg
$newtarget = $newtarget.$newimage['name'];

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($newimage))
{
$errorstring = $errorstring."Upload a jpeg, gif, or bmpWER, ";
$ivalue = $ivalue.$image;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($newtarget))
{
$errorstring = $errorstring."A file with that name already existsERW".$newimage['name'];
$ivalue = $ivalue.$image;
}

//RESIZING IMAGES //

if($newfile_type == "image/pjpeg" || $newfile_type == "image/jpeg"){
$new_image_ = imagecreatefromjpeg($newimage['tmp_name']);
}elseif($newfile_type == "image/x-png" || $newfile_type == "image/png"){
$new_image_ = imagecreatefrompng($newimage['tmp_name']);
}elseif($newfile_type == "image/gif"){
$new_image_ = imagecreatefromgif($newimage['tmp_name']);} // FILE TYPE CHECK FOR 1ST IMAGE

//list the width and height and keep the height ratio.
$new_height = imagesy($new_image_);
$new_width = imagesx ($new_image_);

if ($new_height > 0 && $new_height < $Med_height)
{
$errorstring = $errorstring."The file is too medium ";
$ivalue = $ivalue.$image;
}

if ($new_height > 1500)
{
$newresized_image_large = $resized_img3_large;
$newnew_image_large = $new_img3_large;
$NEWTARGETPATH_LARGE = $TARGET_PATH3_large;
}

if ($new_height > $Med_height)
{
if($newfile_type == "image/pjpeg" || $newfile_type == "image/jpeg")
{$newnew_image_med = imagecreatefromjpeg($newimage['tmp_name']);}
elseif($newfile_type == "image/x-png" || $newfile_type == "image/png")
{$newnew_image_med = imagecreatefrompng($newimage['tmp_name']);}
elseif($newfile_type == "image/gif")
{$newnew_image_med = imagecreatefromgif($newimage['tmp_name']);} // FILE TYPE CHECK FOR MED

if (function_exists(imagecreatetruecolor))
{$newresized_image_med = imagecreatetruecolor($Med_width,$Med_height);}
imagecopyresampled($newresized_image_med, $newnew_image_med, 0, 0, 0, 0, $Med_width, $Med_height, $new_width, $new_height);

# Now put a WATERMARK on

$watermark = imagecreatefrompng('watermark.png');

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);

$dest_x = $Med_width - $watermark_width - 7;
$dest_y = $Med_height - $watermark_height - 5;

imagecopymerge($newresized_image_med, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

$NEWTARGETPATH_MED = $newtarget."_med_".$newimage['name'];
$NEWTARGETPATH_LARGE = $newtarget."_large_".$newimage['name'];
/*

IN SQL YOU SHOULD SAVE IMAGE NAME and IMAGE HEIGHT ONLY,

THEN PULL IT UP IN LIGHTBOX BY SAYING, for example:

A H-REF: <?php echo $NAME."_med".$NAME; ?>

IF (IMAGE1_HEIGHT >350)
{A H-REF: <?php echo $NAME."_large".$NAME; ?> ) */

if($newfile_type == "image/pjpeg" || $newfile_type == "image/jpeg")
{$newnew_image_thumb = imagecreatefromjpeg($newimage['tmp_name']);}
elseif($newfile_type == "image/x-png" || $newfile_type == "image/png")
{$newnew_image_thumb = imagecreatefrompng($newimage['tmp_name']);}
elseif($newfile_type == "image/gif")
{$newnew_image_thumb = imagecreatefromgif($newimage['tmp_name']);} // FILE TYPE CHECK FOR THUMB

if (function_exists(imagecreatetruecolor))
{$newresized_image_thumb = imagecreatetruecolor(140,100);}
else
{$errorstring = $errorstring."GD Library version 2+ Bot available ";
$ivalue = $ivalue.$image; }

//the resizing is going on here!
if (
imagecopyresampled($newresized_image_thumb, $newnew_image_thumb, 0, 0, 0, 0, 140, 100, $new_width, $new_height))
{}
else
{ $errorstring = $errorstring."Couldnt Uplaod Files3 ";
$ivalue = $ivalue.$image; }

$NEWTARGETPATH_THUMB = $newtarget."_thumb_".$image1['name'];


// Lets attempt to move the file from its temporary directory to its new home
if (
ImageJpeg($newresized_image_med, $NEWTARGETPATH_MED, 100) &&
ImageJpeg($newresized_image_thumb, $NEWTARGETPATH_THUMB, 100))
{
}
else
{
// A common cause of file moving failures is because of
// bad permissions on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$errorstring = $errorstring."Couldnt Uplaod Files4 $TARGET_PATH1";
$ivalue = $ivalue.$image;
} #REMEMBER TO REMOVE THIS TO THE OTHER IMAGE UPLOADING FINAL SECTION



}

if ($new_height > $large_height)
{
if($newfile_type == "image/pjpeg" || $newfile_type == "image/jpeg")
{$newnew_image_large = imagecreatefromjpeg($newimage['tmp_name']);}
elseif($newfile_type == "image/x-png" || $newfile_type == "image/png")
{$newnew_image_large = imagecreatefrompng($newimage['tmp_name']);}
elseif($newfile_type == "image/gif")
{$newnew_image_large = imagecreatefromgif($newimage['tmp_name']);} // FILE TYPE CHECK FOR LARGE

if (function_exists(imagecreatetruecolor))
{$newresized_image_large = imagecreatetruecolor($large_width,$large_height);}
imagecopyresampled($newresized_image_large, $newnew_image_large, 0, 0, 0, 0, $large_width, $large_height, $new_width, $new_height);

$watermark2 = imagecreatefrompng('watermark.png');

$watermark_width2 = imagesx($watermark2);
$watermark_height2 = imagesy($watermark2);
$image2 = imagecreatetruecolor($watermark_width2, $watermark_height2);

$bufferw = ($large_width/100);
$bufferh = ($large_height/100) * 2.5;
$dest_x2 = $large_width - $watermark_width2 - $bufferw;
$dest_y2 = $large_height - $watermark_height2 - $bufferh;

imagecopymerge($newresized_image_large, $watermark2, $dest_x2, $dest_y2, 0, 0, $watermark_width2, $watermark_height2, 60);

if (
ImageJpeg($newresized_image_large, $NEWTARGETPATH_LARGE, 100)
)
{
}
else
{
// A common cause of file moving failures is because of
// bad permissions on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$errorstring = $errorstring."Couldnt Uplaod Files42 $TARGET_PATH1";
$ivalue = $ivalue.$image;
}
}
?>
... This is the code for image four in particular, all other images follow this coding aswell.

And here's the code used in the html form image upload page

Code: Select all

if($_POST['submit'])

{
// Start a session for error reporting
session_start();

// Call our connection file
# require("includes/conn.php");


$errorstring = ""; //default value of error string
$pvalue = "";
$nvalue = "";
$dvalue = "";
$ivalue = "";
$checkm = "";
$checkf = "";
$checkb = "";
$checkl = "";
$checkn = "";
$checkm = "";

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

if (in_array($file['type'], $valid_types))
return 1;
return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH1 = "gchomeimages2/";
$TARGET_PATH2 = "gchomeimages2/";
$TARGET_PATH3 = "gchomeimages2/";
$TARGET_PATH4 = "gchomeimages2/";
$TARGET_PATH5 = "gchomeimages2/";
$TARGET_PATH6 = "gchomeimages2/";
$TARGET_PATH7 = "gchomeimages2/";
$TARGET_PATH8 = "gchomeimages2/";

// Get our POSTed variables
$Name = $_POST['Name'];
$Promoter = $_POST['Promoter'];
$Town = $_POST['Town'];
$Gender = $_POST['gender'];
$Description = $_POST['Description'];
$Something = $_POST['Something'];
$image1 = $_FILES['image1'];
$image2 = $_FILES['image2'];
$image3 = $_FILES['image3'];
$image4 = $_FILES['image4'];
$image5 = $_FILES['image5'];
$image6 = $_FILES['image6'];
$image7 = $_FILES['image7'];
$image8 = $_FILES['image8'];


// Sanitize our inputs

$image5['name'] = mysql_real_escape_string($image5['name']);
$image6['name'] = mysql_real_escape_string($image6['name']);
$image7['name'] = mysql_real_escape_string($image7['name']);
$image8['name'] = mysql_real_escape_string($image8['name']);

if (!$image1['name'])
{
$errorstring = $errorstring."Upload a jpeg, gif, or bmp,utru ";
$ivalue = $ivalue.$image1;
}
else
{
$image1_height = imagesy ($image1);
$image1_width = imagesx ($image1);
include ("gchomesimageuploaderdiff_image1.php");
}

if (!$image2['name'])
{
}
else
{
$image2_height = imagesy ($image2);
$image2_width = imagesx ($image2);
include ("gchomesimageuploaderdiff_image2.php");
}

if (!$image3['name'])
{
}
else
{
$image3_height = imagesy ($image3);
$image3_width = imagesx ($image3);
include ("gchomesimageuploaderdiff_image3.php");
}

if (!$image4['name'])
{
}
else
{
$image4_height = imagesy ($image4);
$image4_width = imagesx ($image4);
include ("gchomesimageuploaderdiff_image4.php");
}[/syntax]
[/CODE]
... The code gioes on like this for all 10 pictures .


Can someone please help me? I've looked around for ages and cannot for the life of me work out what is wrong
Last edited by Benjamin on Wed Aug 03, 2011 10:43 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
SabirAhmed
Forum Newbie
Posts: 23
Joined: Tue Aug 02, 2011 11:56 am

Re: Problem uploading and resizing multiple images

Post by SabirAhmed »

Does no-one have any advice? Please guys, could really do with the help here! :S
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Problem uploading and resizing multiple images

Post by genix2011 »

Hi,

what exactly should be done with the uploaded files, you have so much redundant code, that it's really hard to see what you want to do exactly.
SabirAhmed
Forum Newbie
Posts: 23
Joined: Tue Aug 02, 2011 11:56 am

Re: Problem uploading and resizing multiple images

Post by SabirAhmed »

Hi Genix,

there is redundant code, yes. I have written the coding out properly on this post so you can make more sense of it

viewtopic.php?f=1&t=130898

Basically the coding should just detect how big the image is ... if the image height is less than 1500, it should just create a medium sized copy and a thumbnail sized copy (so 2 copies), and if the image is over 1500 height, it should create a medium, thumb and large sized copy (so 3 copies).

Hope that makes sens,e thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Problem uploading and resizing multiple images

Post by pickle »

Some tips to get feedback:

- Wrap your code in tags so it doesn't scroll forever, and is highlighted neatly
- Try to be more succinct with your code. I see all that code & don't want to be bothered trying to sort it out.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
SabirAhmed
Forum Newbie
Posts: 23
Joined: Tue Aug 02, 2011 11:56 am

Re: Problem uploading and resizing multiple images

Post by SabirAhmed »

Hi pickle,

yes I have only recently learnt how to add the code window here, thanks for the info.
Post Reply