...

View Full Version : weird image upload (resize and crop) behavior



BroChris
12-25-2006, 06:33 PM
This used to work on my old server, but I recently switched and can't get it to work right anymore. When you upload a picture, it appears to have worked (it shows the cropped image), but when you go to the page where the image is supposed to be displayed, it just shows a black box. The smaller thumbnail (on the home page) is also just a black box. The best way to see what I mean is just test the whole process: http://www.100mostbeautifulbabies.com/add-your-baby.html

Here's relevant portions of the code. From the add-your-baby page:


<form action="uploadpicture.html?action=upload&amp;name=<?php echo $file ?>&amp;i=<?php echo $i; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10484576" />
<input type="file" id="picture" name="picture" />
<input type="submit" name="submit" value="Send" />
</form>

The variables $file (the baby's name without spaces) and $i (a number) were posted from a previous form. A directory was created called $file and set to 0777.

Then from the uploadpicture page:


<?php

$action = $_GET['action'];
$name = $_GET['name'];
$i = $_GET['i'];

if ($action == 'upload'){
$nw = '60';
$nh = '60';
$nbw = '200';
$nbh = '200';

$ipath = "/home/babies/public_html/$name";
$tpath = "/home/babies/public_html/images";

$img = $_FILES['picture']['tmp_name'];
$dimensions = GetImageSize($img);

$biname = "$ipath/$name.jpg";
$thname = "$tpath/$i.jpg";

$w = $dimensions[0];
$h = $dimensions[1];

$img2 = ImageCreateFromJpeg($img);
$thumb = ImageCreateTrueColor($nw,$nh);

$wm = $w/$nbw;
$hm = $h/$nbh;

$h_height = $nbh/2;
$w_height = $nbw/2;

if($w > $h){
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;

$bigimg=ImageCreateTrueColor($nbw,$nbh);
ImageCopyResampled($bigimg,$img2,-$int_width,0,0,0,$adjusted_width,$nbh,$w,$h);
ImageJPEG($bigimg,$biname,65);
}
elseif(($w < $h) || ($w == $h)){
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;

$bigimg = ImageCreateTrueColor($nbw,$nbh);
ImageCopyResampled($bigimg,$img2,0,-$int_height,0,0,$nbw,$adjusted_height,$w,$h);
ImageJPEG($bigimg,$biname,65);
}
else{
$bigimg = ImageCreateTrueColor($nbw,$nbh);
ImageCopyResampled($bigimg,$img2,0,0,0,0,$nbw,$nbh,$w,$h);
ImageJPEG($bigimg,$biname,65);
}

$wm = $w/$nw;
$hm = $h/$nh;

$h_height = $nh/2;
$w_height = $nw/2;

if($w > $h){
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;

ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
ImageJPEG($thumb,$thname,65);
}
elseif(($w < $h) || ($w == $h)){
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;

ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
ImageJPEG($thumb,$thname,65);
}
else{
ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);
ImageJPEG($thumb,$thname,65);
}

imagedestroy($img2);
}
?>

<p class="addingsteps"><span>Step 1: Enter Details</span><span>Step 2: Upload Picture</span><span><strong>Step 3: Done!</strong></span></p>
<p>All done! Your baby should now be up on the <a href="index.html">home</a> page. Check back often to see all of the other beautiful babies too! Below is the picture that you submitted.</p>
<img src="<?php echo $name . '/' . $name; ?>.jpg" width="200" height="200" alt="<?php echo $name; ?>"><br />
<p>Be sure to <a href="tell-a-friend.html">tell your friends</a> to <a href="<?php echo $name; ?>/">see your baby online</a>!</p>

If you upload a picture, and then hit refresh after the page has loaded, it works the second time.

Here is my phpinfo (http://www.100mostbeautifulbabies.com/phpinfo.php) if that is of any use. Any help is greatly appreciated.

edit: The error_log may help diagnose the problem also:


[25-Dec-2006 12:47:12] PHP Warning: Division by zero in /home/babies/public_html/uploadpicture.html on line 46
[25-Dec-2006 12:47:12] PHP Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/babies/public_html/uploadpicture.html on line 51
[25-Dec-2006 12:47:12] PHP Warning: Division by zero in /home/babies/public_html/uploadpicture.html on line 75
[25-Dec-2006 12:47:12] PHP Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/babies/public_html/uploadpicture.html on line 79
[25-Dec-2006 12:47:12] PHP Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/babies/public_html/uploadpicture.html on line 87

Fumigator
12-26-2006, 04:22 PM
Did your path change at all from the old to new server?

Your error log tells you the problem... whatever $img2 points to isn't an image resource. Add tons of echos and verify each step of your code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum