...

View Full Version : Dynamic Thumbnail problems



tribalmaniac
03-06-2007, 08:04 PM
I haven't got much experience in workign with images in php, but i found a code and played around with it and I have this:


<?php
include("top.php");

function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 20) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

$random_digit = createRandomPassword();
define('NORMAL_MAX_WIDTH',400); //the maximum width of a thumnail in pixels
define('NORMAL_MAX_HEIGHT',400); //the maximum height of a thumbnail in pixels
define('NORMAL_DIR','userimages/nt/'); //the directory to store thumbnails in
define('THUMB_MAX_WIDTH',170); //the maximum width of a thumnail in pixels
define('THUMB_MAX_HEIGHT',170); //the maximum height of a thumbnail in pixels
define('IMAGE_DIR','userimages/'); //the directory to store images in
define('THUMB_DIR','userimages/thumbs/'); //the directory to store thumbnails in
define('THUMB_MINI_WIDTH',80); //the maximum width of a thumnail in pixels
define('THUMB_MINI_HEIGHT',80); //the maximum height of a thumbnail in pixels
define('MINI_DIR','userimages/mini/'); //the directory to store thumbnails in
define('IMAGE_PREFIX',$random_digit); //string to preface thumbnail names with
define('THUMB_PREFIX','th_' . $random_digit . ''); //string to preface thumbnail names with


//if the directories do not exist display an error and exit
if(!is_dir(IMAGE_DIR) || !is_dir(THUMB_DIR)) {
echo 'Image Direcory (' . IMAGE_DIR . ') or Thumb Dir (' . THUMB_DIR . ') does not exist.';
exit;
} //end if

//if the form is being submitted
if(isset($_POST['submitter'])) {
$errors = make_thumb($_FILES['image_file']);
$errors = make_normal($_FILES['image_file']);
$errors = make_mini($_FILES['image_file']);
} //end if

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head>
<body>
<?php
$action = $_REQUEST["action"];
if($action == ""){

$imagesSql = mysql_query("SELECT * FROM `pictures` WHERE `userid` = '".$_SESSION["id"]."'");
$imagecount = mysql_num_rows($imagesSql);
if($imagecount <= 10){
?>
<form action="<?=$_SERVER['PHP_SELF']?>" name="uploadimage" method="post" enctype="multipart/form-data">
<table style="width: 398px;" cellpadding="1" border="0">
<tr>
<td valign="middle" align="right" style="height: 10px; width: 150px;">
<p style="font-size: 11px; height: 10px; margin-top: 0px; margin-bottom: 0px;">Image (jpg only): </p>
</td>
<td valign="middle" align="left" style="height: 10px; margin-top: 0px; margin-bottom: 0px;">
<input type="file" name="image_file" class="regform" />
</td>
</tr>
<tr>
<td valign="middle" align="center" style="height: 55px; margin-top: 0px; margin-bottom: 0px;" colspan="2">
<input type="submit" value="Upload" class="regform" name="submitter" style="width: 100px; border: 1px solid #CCCCCC; background-color:#000000; color:#FFFFFF; cursor:pointer;" />
</td>
</tr>



</table>
</form>
<?php
}else{
echo('<p align="center">You have already reached your photo upload limit!</p>');
}
}
?>
</div>
<p></p>
<?php
//if the form is being submitted
if(isset($_POST['submitter'])) {
//if the function generated any errors display them
if($errors != 1) {
echo $errors;
//otherwise display the two generated images
} else {
session_start();
$imagesSql = mysql_query("SELECT * FROM `pictures` WHERE `userid` = '".$_SESSION["id"]."'");
$imagecount = mysql_num_rows($imagesSql);

$userid = $_SESSION['id'];
$image = IMAGE_PREFIX . $_FILES['image_file']['name'];
$default = 0;
if($imagecount == 0){
$default = 1;
}

mysql_query("INSERT INTO pictures (`userid`, `image`, `default`) VALUES ('$userid', '$image', '$default')") or die(mysql_error());

$o_img = @getimagesize(IMAGE_DIR . IMAGE_PREFIX . $_FILES['image_file']['name']);
$n_img = @getimagesize(THUMB_DIR . THUMB_PREFIX . $_FILES['image_file']['name']);

echo('<p align="center">Thank-you for uploading this image<br /><a href="galleryinput.php">Click here</a> to upload another</p>');

} //end if
}
?>

</body>
</html>
<?php
function make_thumb($uploaded_file_array) {
//check that the uploaded file is a valid filetype
if(!@eregi('jpeg|png',$uploaded_file_array['type'])) {
return "***ERROR: This program accepts only .jpg and .png format images.<br />\n";
} //end if

//copy the file from the temp location to it's permanent location safely
if(!@move_uploaded_file($uploaded_file_array['tmp_name'],
IMAGE_DIR . IMAGE_PREFIX . $uploaded_file_array['name'])) {
return "***ERROR: Could not copy the upload file from temporary location.<br />\n";
}

//initialize the filename and thumbnail name variables
$im_file_name = IMAGE_DIR . IMAGE_PREFIX . $uploaded_file_array['name'];
$th_file_name = THUMB_DIR . THUMB_PREFIX . $uploaded_file_array['name'];

//get the attributes of the uploaded image
$image_attribs = @getimagesize($im_file_name);

//copy the used attributes into more readable variable names
$width = $image_attribs[0];
$height = $image_attribs[1];
$type = $image_attribs[2];

//calculate the ratio to decrease the image size by maintining orientation
$ratio = ($width > $height) ? THUMB_MAX_WIDTH/$width : THUMB_MAX_WIDTH/$height;

//calculate the dimensions of the thumbnail
$th_width = $width * $ratio;
$th_height = $height * $ratio;

//create the thumbnail image
$im_new = @imagecreatetruecolor($th_width,$th_height);
@imageAntiAlias($im_new,true);

switch($type) {
//if we are working with a jpeg image
case 2:
//read the original file into an image resource
$im_old = @imageCreateFromJpeg($im_file_name);
//copy the original image to the thumbnail image and resize it
@imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height,$width,$height);
//write out the thumbnail image
@imageJpeg($im_new,$th_file_name,100);
break;
//if we are working with a png image
case 3:
//read the original file into an image resource
$im_old = @imageCreateFromPng($im_file_name);
//copy the original image to the thumbnail image and resize it
@imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height,$width,$height);
//write out the thumbnail image
@imagePng($im_new,$th_file_name);
break;
} //end switch
//clean up after ourselves
@imagedestroy($im_old);
@imagedestroy($im_new);

//return 1 for success
return 1;
} //end make thumb

function make_mini($uploaded_file_array) {

$im_file_name = IMAGE_DIR . IMAGE_PREFIX . $uploaded_file_array['name'];
$th_file_name = MINI_DIR . IMAGE_PREFIX . $uploaded_file_array['name'];

//get the attributes of the uploaded image
$image_attribs = @getimagesize($im_file_name);

//copy the used attributes into more readable variable names
$width = $image_attribs[0];
$height = $image_attribs[1];
$type = $image_attribs[2];

//calculate the ratio to decrease the image size by maintining orientation
$ratio = ($width > $height) ? THUMB_MINI_WIDTH/$width : THUMB_MINI_WIDTH/$height;

//calculate the dimensions of the thumbnail
$th_width = $width * $ratio;
$th_height = $height * $ratio;

//create the thumbnail image
$im_new = @imagecreatetruecolor($th_width,$th_height);
@imageAntiAlias($im_new,true);

switch($type) {
//if we are working with a jpeg image
case 2:
//read the original file into an image resource
$im_old = @imageCreateFromJpeg($im_file_name);
//copy the original image to the thumbnail image and resize it
@imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height,$width,$height);
//write out the thumbnail image
@imageJpeg($im_new,$th_file_name,100);
break;
//if we are working with a png image
case 3:
//read the original file into an image resource
$im_old = @imageCreateFromPng($im_file_name);
//copy the original image to the thumbnail image and resize it
@imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height,$width,$height);
//write out the thumbnail image
@imagePng($im_new,$th_file_name);
break;
} //end switch
//clean up after ourselves
@imagedestroy($im_old);
@imagedestroy($im_new);

//return 1 for success
return 1;
} //end make thumb

function make_normal($uploaded_file_array) {

//initialize the filename and thumbnail name variables
$im_file_name = IMAGE_DIR . IMAGE_PREFIX . $uploaded_file_array['name'];
$th_file_name = NORMAL_DIR . IMAGE_PREFIX . $uploaded_file_array['name'];

//get the attributes of the uploaded image
$image_attribs = @getimagesize($im_file_name);

//copy the used attributes into more readable variable names
$width = $image_attribs[0];
$height = $image_attribs[1];
$type = $image_attribs[2];

//calculate the ratio to decrease the image size by maintining orientation
$ratio = ($width > $height) ? NORMAL_MAX_WIDTH/$width : NORMAL_MAX_WIDTH/$height;

//calculate the dimensions of the thumbnail
$th_width = $width * $ratio;
$th_height = $height * $ratio;

//create the thumbnail image
$im_new = @imagecreatetruecolor($th_width,$th_height);
@imageAntiAlias($im_new,true);

switch($type) {
//if we are working with a jpeg image
case 2:
//read the original file into an image resource
$im_old = @imageCreateFromJpeg($im_file_name);
//copy the original image to the thumbnail image and resize it
@imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height,$width,$height);
//write out the thumbnail image
@imageJpeg($im_new,$th_file_name,100);
break;
//if we are working with a png image
case 3:
//read the original file into an image resource
$im_old = @imageCreateFromPng($im_file_name);
//copy the original image to the thumbnail image and resize it
@imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height,$width,$height);
//write out the thumbnail image
@imagePng($im_new,$th_file_name);
break;
} //end switch
//clean up after ourselves
@imagedestroy($im_old);
@imagedestroy($im_new);

//return 1 for success
return 1;
} //end make thumb

?>



Really sorry if it is unreadable!

Basically I need 3 images being made automatically, When I ran this on my server it worked fine, but as soon as i moved it to my friends, the images dont work and I get a division by zero error?

Just wondering if anyone has any ideas on what the problem could be?

Fumigator
03-06-2007, 08:28 PM
It would help if you copy/pasted the error since it has the line number in it and we could see where in the script the error occured.

Looking at the script though I can come up with a theory that the value of $width and/or $height is zero, which is because the get_image_size() function failed (which you wouldn't know because errors are being suppressed due to the "@" before the function call) and that is probably due to the fact that your constant definitions that define the directories are either wrong, the directories aren't there, or you don't have permission to write to those directories.

This theory is supported further by the fact the script works fine in another environment but in this particular environment where you didn't set up your directories properly it doesn't work.

Too bad the script doesn't have validation checks (with clear error messages) on the directory names before it gets into the thick of it. Naughty free script writer!

tribalmaniac
03-06-2007, 08:35 PM
It would help if you copy/pasted the error since it has the line number in it and we could see where in the script the error occured.

Looking at the script though I can come up with a theory that the value of $width and/or $height is zero, which is because the get_image_size() function failed (which you wouldn't know because errors are being suppressed due to the "@" before the function call) and that is probably due to the fact that your constant definitions that define the directories are either wrong, the directories aren't there, or you don't have permission to write to those directories.

This theory is supported further by the fact the script works fine in another environment but in this particular environment where you didn't set up your directories properly it doesn't work.

Too bad the script doesn't have validation checks (with clear error messages) on the directory names before it gets into the thick of it. Naughty free script writer!

Ahh thank-you so much! This has been annoying me so much!
It was the permissions on the folders :)

Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum