Page 1 of 1

thumb script

Posted: Mon Sep 13, 2004 7:53 pm
by 1stMoogwai
feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hello. I have searched the thumbnail scripts and threads here and haven't found anything to point me in the right direction yet. I am having GREAT difficulties getting this script to work. THe full sized image upload is working... the thumbnail is NOT AT ALL. Any help would be greatly appreciated. 
Right now the line:

Code: Select all

$src_img=imagecreatefromjpeg($fullNameThumb);
Returns 4 errors. All stemming from the fact that it is not finding the file/path associated with $fullNameThumb. If I substitute $fullName for $fullNameThumb, there are no erros reported but the thumb script produces nothing.
I tried using the normal copy function with $fullNameThumb (you'll see it commented out) to try and create the image for temporary use in the thumb script... No luck.
I have checked and the GD library is installed and the proper version too, phpinfo();
here is the entire script thus far:

Code: Select all

if (@$file) { 
        //declare var for photo file name for db 
        $photo_file = $file['name']; 
        //sql 
        $query = "    INSERT into photos (photo_file, photo_date, photo_desc) 
                    VALUES ('$photo_file', '$date', '$desc')"; 
        $result = mysql_query($query, $conn) or die(mysql_error()); 
         
         
        //path name vars 
        $abspath = $HTTP_SERVER_VARS['PATH_TRANSLATED']; 
        $stub=ereg_replace("/photoAdd.php","/",$abspath); 
     
        // use $fullname for the resulting path for file uploads, based on any changes you make above 
        $fullname = $stub ."/images/photos/".$file['name']; 
        $fullNameThumb = $stub ."/images/photos/thumbs/".$file['name']; 
         
      //copies full sized image 
        copy($file['tmp_name'],$fullname); 
        //copy($file['tmp_name'],$fullNameThumb); 
         
        //begin thumbnail script 
        $src_img=imagecreatefromjpeg($fullNameThumb);  
        $old_x=imagesx($src_img);  
        $old_y=imagesy($src_img);  
        $thumb_w=50; 
        $thumb_h=50; 
        $dst_img = imagecreatetruecolor($thumb_w,$thumb_h);  
        //echo $src_img; 
         imagecopyresampled($dst_img,$src_img,0,0,0,0,$th
umb_w,$thumb_h,$old_x,$old_y);  
        //imagedestroy($dst_img); 
        //imagedestroy($src_img); 
    }
Thanks in advance for any help.
1M.


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Sep 13, 2004 8:14 pm
by feyd
Moved to Graphics.

Posted: Mon Nov 29, 2004 2:20 pm
by GhostXL
Make 2 files:

file 1 (html, call it what ever u want):

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>

<body>
<body>
<form enctype="multipart/form-data" method="post" action="uploadandresize.php">
  <p>
    Bestand: 
    <input type="file" name="userfile">
</p>
  <p>
    Doelmap: 
    <input type="text" name="doelmap">
</p>
  <p>
    Betandsnaam: 
      <input type="text" name="bestandsnaam">
</p>
  <p>
    <input type="submit" name="Submit" value="uploaden">
</p>
</form>
</body>
</body>
</html>
File 2 (call it uploadandresize.php)

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>
<body>
Bestand: <? echo $userfile; ?><br>
Bestands naam: <? echo $userfile_name; ?><br>
Bestands grote: <? echo $userfile_size; ?><br>
Bestands type: <? echo $userfile_type; ?><br>
<br>
Doelmap: <? echo $doelmap; ?><br>
Nieuwe bestands naam: <? echo $bestandsnaam; ?><br>
<p>&nbsp;</p>

<?php
// look if file with same name allready exists
<?php
// resize to thumbnail script
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($userfile);

if ($width && ($width_orig < $height_orig)) &#123;
   $width = ($height / $height_orig) * $width_orig;
&#125; else &#123;
   $height = ($width / $width_orig) * $height_orig;
&#125;
// Resample
// if GD 2.0.l or greater is available use imagecreatetruecolor() instead of imagecreate()
$image_p = imagecreate($width, $height);
$image = imagecreatefromjpeg($userfile);
// if GD 2.0.l or greater is available use imagecopyresampled() instead of imagecopyresized()
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// save resized picture in desination folder
imagejpeg($image_p, $doelmap."/thumb".$bestandsnaam, 85);
?>

<?php
// save uploaded picture in destination folder
if (rename($userfile,$doelmap."/".$bestandsnaam)==true)&#123;
	echo "Bestand is met succes geupload<br>";
&#125; else &#123;
	echo "Er is een fout opgetreden bij het uploaden van het bestand<br>";
&#125;
?>
</body>
</html>
IMPORTANT make sure u upload to a folder u have permission for!!


Sorry that part of the code's in dutch...........
Any way the code should upload both the original and the thumbnail.
The thumbnail will look crap unless u use GDlib 2.0.1 or higher and use imagecopyresampled() instead of imagecopyresized() and imagecreatetruecolor() instead of imagecreate().

Hope it helps

Posted: Mon Nov 29, 2004 4:06 pm
by kettle_drum
Check out my file upload class in the code snippet section as it does all that you want.

Posted: Tue Nov 30, 2004 3:23 am
by phpScott
echo out

Code: Select all

<?php
$fullname = $stub ."/images/photos/".$file['name']; 
$fullNameThumb = $stub ."/images/photos/thumbs/".$file['name']; 
?>
what are there values are they pointing to the right directories that you want?

you also appear to be missing saving the file add

Code: Select all

<?php
imageJpeg($dst_img, $fullNameThumb);
?>
after your imagecopyresamples().
http://uk.php.net/manual/en/function.imagejpeg.php