Forum Home
Press F1
 
Thread ID: 61263 2005-08-30 06:04:00 PHP - resize pictures on submission Morgenmuffel (187) Press F1
Post ID Timestamp Content User
1357369 2005-09-01 04:23:00 I'm back

Ok I have got it uploading the image and storing it ib the correct directory,

now i am trying to create a thumbnail of the image and store it in a subdirectory

but it isn't working

see here (www.nigel.geek.nz)

I just get garbage out the end

(and IE is playing silly buggers, it wont accept any jpg images, jpgs are accepted be firefox and opera though)
Morgenmuffel (187)
1357370 2005-09-01 04:33:00 Do the routines (like move_uploaded_file()) return result codes? I notice in your first posting that you were getting an error message (presumably for a fatal error) then, unconditionally, the success message. ;) This is not a good thing. If there's a result code, it's a Very Good Idea to check it. If you don't it will always bite you. Always. If there aren't any result codes, you will be bitten frequently and painfully. Graham L (2)
1357371 2005-09-01 06:24:00 Thanks all It all works fine now

Had to change the code around a bit but it went in the end

Thanks all

BUGGER

There is still a problem with IE not accepting some file types

go here and try

www.nigel.geek.nz
Morgenmuffel (187)
1357372 2005-09-01 06:56:00 This is the code that checks the size of the image

it works with fiefox and opera but not IE, I don't understand why, as it is server side code and not browser specific

IE will accept gifs up to 5K, but it won't accept jpgs




if (//begin condition
(($_FILES['file']['type'] == 'image/gif') && ($_FILES['file']['size'] < 5000)) ||
(($_FILES['file']['type'] == 'image/jpeg') && ($_FILES['file']['size'] < 75000))
)//end condition
{
Morgenmuffel (187)
1357373 2005-09-01 07:02:00 Is PHP case-sensitive? MS might send things in uppercase? Graham L (2)
1357374 2005-09-01 07:21:00 PHP is case sensitive, but I've never had a problem with case sensitivity and as it works for the smaller sized gifs I very much doubt it will be case sensitivity

Cheers
Morgenmuffel (187)
1357375 2005-09-01 07:35:00 Heres a bit of code from one of my sites, slightly different than the way yours is laid out but you may be able to pick bits out of it



// add image to images table
if($image) {
// generate icode
$icode = substr(time().rand(10000,99999),-15);

// copy image to temp folder
$tempname = './temp/'.$icode.'TEMP.JPG';
copy($image, $tempname);
unlink($image);

// get image properties
$properties = getimagesize($tempname);
if($properties[2] == 2) { // if the image is a .jpg
$source = imagecreatefromjpeg($tempname); // create image identifier
$imagex = imagesx($source);
$imagey = imagesy($source);

// copy image to images folder
$imagename = $icode.'IMG.JPG'; // this will be stored in db
$image_loc = "./images/$imagename";
copy($tempname, $image_loc);
unlink($tempname);

// resize the image if neccessary
if($imagex > 400) {
$newy = round((400 * $imagey) / $imagex);
//echo "imagex = $imagex<br>imagey = $imagey<br>newy = $newy<br>"; exit(); // TEST
resize($image_loc, 400, $newy, $image_loc);
}

// create thumbnail
$thumbname = $icode.'TMB.JPG'; // this will be stored in db
$thumb_loc = "./thumbs/$thumbname";

$thumbx = $maxx;
$thumby = round(($imagey * $thumbx) / $imagex);

if($thumby > $maxy) {
$thumbx = round(($thumbx * $maxy) / $thumby);
$thumby = $maxy;
}

if(resize($image_loc, $thumbx, $thumby, $thumb_loc)) {
// store data
$link = mysql_connect($dbhost, $dbuser, $dbpass);
$query = "INSERT INTO $dbimg VALUES('0','$ccode','$thumbname','$imagename')";
mysql_db_query($dbname, $query, $link);

// update numimages
$query = "UPDATE $dbvin SET numimages=numimages+1 WHERE ccode='$ccode'";
mysql_db_query($dbname, $query, $link);

mysql_close($link);

// jump back to summary page
echo "<script language='JavaScript'> window.location='summary.php?ccode=$ccode'; </script>";
exit();
}
} @ unlink($tempname); // delete the temp file if an error occurs
}
?>

The above relies on another 3 other pages to work correctly one of which is below
Hope it's of some help


<?
require 'config/config.php';
require 'functions.php';
require 'global.php';
require 'secure.php';
// add image to images table
if($image) {
// generate icode
$icode = substr(time().rand(10000,99999),-15);
// copy image to temp folder
$tempname = './temp/'.$icode.'TEMP.JPG';
copy($image, $tempname);
unlink($image);

// get image properties
$properties = getimagesize($tempname);
if($properties[2] == 2) { // if the image is a .jpg
$source = imagecreatefromjpeg($tempname); // create image identifier
$imagex = imagesx($source);
$imagey = imagesy($source);

// copy image to images folder
$imagename = $icode.'IMG.JPG'; // this will be stored in db
$image_loc = "./images/$imagename";
copy($tempname, $image_loc);
unlink($tempname);

// resize the image if neccessary
if($imagex > 400) {
$newy = round((400 * $imagey) / $imagex);
//echo "imagex = $imagex<br>imagey = $imagey<br>newy = $newy<br>"; exit(); // TEST
resize($image_loc, 400, $newy, $image_loc);
}

// create thumbnail
$thumbname = $icode.'TMB.JPG'; // this will be stored in db
$thumb_loc = "./thumbs/$thumbname";
$thumbx = $maxx;
$thumby = round(($imagey * $thumbx) / $imagex);

if($thumby > $maxy) {
$thumbx = round(($thumbx * $maxy) / $thumby);
$thumby = $maxy;
}

if(resize($image_loc, $thumbx, $thumby, $thumb_loc)) {
// store data
$link = mysql_connect($dbhost, $dbuser, $dbpass);
$query = "INSERT INTO $dbimg VALUES('0','$ccode','$thumbname','$imagename')";
mysql_db_query($dbname, $query, $link);

// update numimages
$query = "UPDATE $dbvin SET numimages=numimages+1 WHERE ccode='$ccode'";
mysql_db_query($dbname, $query, $link);

mysql_close($link);

// jump back to summary page
echo "<script language='JavaScript'> window.location='summary.php?ccode=$ccode'; </script>";
exit();
}
} @ unlink($tempname); // delete the temp file if an error occurs
}
?>
<html>
<head>
<title>blah blah - Add Image to Listing<</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/default.css" rel="stylesheet" type="text/css">
</head>
<body>
<table class='trim' width=700 height=400 border=0 cellpadding=3 cellspacing=0>
<tr>
<td align='center' valign='middle'>
<b>An error has occurred.</b><br>
<br>
<a class='link' href='cpanel.php'><b>Click here to return to the Control Panel</b></a>
</td>
</tr>
</table>
<table width=700 border=0 cellpadding=5 cellspacing=0><tr><td align='center'><? require 'footer.php'; ?></td></tr></table>
</body>
</html>

Excuse the messy coding, I'll get round to a tidy up one day
bartsdadhomer (80)
1357376 2005-09-01 08:13:00 Thanks bartsdadhomer

But I am trying to figure out what is causing the jpgs to fail in IE, when the code is server side PHP



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if (//begin condition
(($_FILES[ ' file ' ][ ' type ' ] == ' image/gif ' ) && ($_FILES[ ' file ' ][ ' size ' ] < 5000)) ||
(($_FILES[ ' file ' ][ ' type ' ] == ' image/jpeg ' ) && ($_FILES[ ' file ' ][ ' size ' ] < 75000))
)//end condition
{
echo ' Return Code: ' . $_FILES[ ' file ' ][ ' error ' ] . ' <br /> ' ;
echo ' Uploading ' . $_FILES[ ' file ' ][ ' name ' ] . ' ( ' .
$_FILES[ ' file ' ][ ' type ' ] . ' , ' .
ceil($_FILES[ ' file ' ][ ' size ' ] / 1024) . ' Kb) . <br /> ' ;
echo "File is temporarily stored as " . $_FILES["file"]["tmp_name"] . ' <br><br> ' ;

if (file_exists( ' imgs/ ' . $_FILES[ ' file ' ][ ' name ' ]))
{
echo $_FILES[ ' file ' ][ ' name ' ] . ' already exists . ' ;
echo ' Please delete the destination file and try again . <br><br> ' ;
}
else
{
move_uploaded_file($_FILES[ ' file ' ][ ' tmp_name ' ], ' imgs/ ' . $_FILES[ ' file ' ][ ' name ' ]);
echo ' File has been stored in your imgs directory . <br><br> ' ;
?>
<form action="index . php" method="post">
<input name="image_name" type="hidden" value="<?= $_FILES[ ' file ' ][ ' name ' ] ?>">
<input name="bob" type="submit">
</form>
<?php
}
}
else
{
echo ' Return Code: ' . $_FILES[ ' file ' ][ ' error ' ] . ' <br /> ' ;
echo ' Sorry, we only accept . gif images under 5Kb for upload . <br> ' ;
echo ' Sorry, we only accept . gif images under 75Kb for upload . <br> ' ;
echo ' file size -- ' . $_FILES[ ' file ' ][ ' size ' ];
}
?>
</body>
</html>
Morgenmuffel (187)
1357377 2005-09-01 09:40:00 What have you changed?
I uploaded a jpg of a hard drive earlier on and it accepted it and just tried again and it won't accept it now
bartsdadhomer (80)
1357378 2005-09-01 10:08:00 Aren't you going to have to create an identifier for each image type then create a seperate function to handle the image type identified
eg


//Identify Image type

$len = strlen($image_name);
$pos =strpos($image_name,".");
$type = substr($image_name,$pos + 1,$len);

if ( $type=="jpeg" || $type=="jpg")
{
thumb_jpeg ($image_name); //Call to jpeg function
}
else if($type="gif" || $type="gif")
{
thumb_gif ($image_name); //Call to gif function
}

echo "<b>Done........</b>";


Function



}
//JPEG function
function thumb_jpeg($image_name)
{
global $source_path;
global $destination_path;

global $new_width;
global $new_height;

$destimg=ImageCreate($new_width,$new_height) or die("Problem In Creating image");

$srcimg=ImageCreateFromJPEG($source_path.$image_na me) or die("Problem In opening Source Image");

ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_wid th,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");

ImageJPEG($destimg,$destination_path.$image_name) or die("Problem In saving");
}

//gif function
function thumb_gif($image_name)
{
global $source_path;
global $destination_path;

global $new_width;
global $new_height;

$destimg=ImageCreate($new_width,$new_height) or die("Problem In Creating image");

$srcimg=ImageCreateFromGif($source_path.$image_nam e) or die("Problem In opening Source Image");

ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_wid th,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");

Imagegif($destimg,$destination_path.$image_name) or die("Problem In saving");
}

this code is for creating thumbnails but shows how image types are uniquely identified
bartsdadhomer (80)
1 2 3 4