Jump to content


Photo

saving a thumbnail


  • Please log in to reply
15 replies to this topic

#1 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 10 April 2006 - 10:31 PM

<?php
// The file
$filename = 'http://www.pwncaeks.com/gallery/cantlogin.JPG';

// Set a maximum height and width
$width = 100;
$height = 100;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>

with that code, i can create the picture in the $filename variable but when its outputted, i need it to be saved.. i cant figure out how to do this..

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 April 2006 - 11:28 PM

The second parameter to the imagejpeg() function, if given, should be a string containing a file name.

See the [a href=\"http://www.php.net/imagejpeg\" target=\"_blank\"]manual entry[/a] form imagejpeg().

Ken

#3 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 10 April 2006 - 11:34 PM

thanks for your reply, but when i put a something in there, i just get the exact file name of my php file displayed.. no image is saved or anything.. just.. [a href=\"http://www.pwncaeks.com/dashboard/testgall3.php\" target=\"_blank\"]http://www.pwncaeks.com/dashboard/testgall3.php[/a]

thats all..

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 April 2006 - 11:54 PM

Please post the code that give that result.

Ken

#5 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 10 April 2006 - 11:55 PM

imagejpeg($image_p, "http://www.pwncaeks.com/gallery/", 100);


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 12:00 AM

Do not put a URL for the name of the file. Put in a local filename.

Ken

#7 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 April 2006 - 12:04 AM

im not quite sure what you mean..

#8 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 11 April 2006 - 12:09 AM

you dnt want to put http:// yadda yadda yadda, you want to put the relative path to your file from the page that generates that script. for instance if you use apache. then your website should be in your htdocs folder. if that page is in that folder and your image in in a file name 'images' in htdocs, then you would want to put /images/filename.jpg rather than that url.

#9 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 April 2006 - 12:15 AM

imagejpeg($image_p, "/usr/home/pwncae/public_html/gallery/thumb_image.jpg", 100);

im using that now.. the image is created succesfully.. however, this "http://www.pwncaeks..../testgall3.php" is still output to the user.. i would prefer that instead of that.. i could write like.. your image was uploaded.. thanks.. blah blah

#10 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 11 April 2006 - 12:20 AM

oh i see what your saying now... are you using apache?

#11 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 April 2006 - 12:25 AM

not sure.. im using a seperate host..

also.. how can i name the new image based off the name of the original image

#12 sdaniels

sdaniels
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 11 April 2006 - 12:33 AM

[!--quoteo(post=363510:date=Apr 10 2006, 07:25 PM:name=insrtsnhere13)--][div class=\'quotetop\']QUOTE(insrtsnhere13 @ Apr 10 2006, 07:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
not sure.. im using a seperate host..

also.. how can i name the new image based off the name of the original image
[/quote]

im confued again, i thought you meant that when they clicked a link they were getting a directory rather than a page. in wich case i was going to ask if you had you conf file set to parse correctly, but this is not the case.
could we see the code for testgall3.php? I think i understand what your doing, if im correct you want a user to upload an image, then you want it to verify that it has been recieved and echo out some message stating that you have recieved it. and currently it is echoing a url back to the user?

#13 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 April 2006 - 12:43 AM

you're basically correct.. i want them to upload it, have the uploaded image resized to a more managable size, then have that file saved.. filename based off the original uploaded file.. then, i want a thank you msg to be displayed telling the user it was uploaded.. and then a link to send them back to the upload page to do it again...

<?php

if (isset($_FILES['userfile']['tmp_name'])){
$filename = $_FILES['userfile']['tmp_name'];

// Set a maximum height and width
$width = 100;
$height = 100;
$caption = $_POST['caption'];

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, "/usr/home/pwncae/public_html/gallery/image.jpg", 100); }else{
?>
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="3000000"> 
Image:<br><input name="userfile" type="file"><br>
Caption:<br><input type="text" name="caption" size="40"><br>
<input type="submit" name="upload" value="Upload">
<? }

?> 


#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 01:52 AM

I took your code and ran it locally on my machine. With a few minor modifications it ran fine.
  • Remove this line:
    <?php header('Content-type: image/jpeg'); ?>
    since it is not needed when you're saving the image to a file.
  • The original name of the file is stored in $_FILES['userfile']['name']
  • You should use a relative path name for your gallery, so that if you move your code it will still work. Something like:
    <?php imagejpeg($image_p, 'gallery/' . $_FILES['userfile']['name'], 100); ?>
  • Make sure the directory where you're storing the thumbnail images is writable by the web server.
  • If you add this code:
    <?php
    echo 'Thumbnail image saved as gallery/' . $_FILES['userfile']['name'] . '<br>';
    echo '<img src="gallery/' . $_FILES['userfile']['name'] .'" height="' . $height . '" width="' . $width . '" alt="' . $_POST['caption'] . '">'; 
    ?>
    right after storing the thumbnail image and before the "else", then you can see that it worked.
You can see my version of your code in action at [a href=\"http://www.rbnsn.com/phpfreaks/savingthumbs.php\" target=\"_blank\"]http://www.rbnsn.com/phpfreaks/savingthumbs.php[/a]
Ken

#15 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 April 2006 - 11:08 AM

thanks, i knew that damn header was ruining everything, and i found a way to name images better than i had before.. whenever a use uploads an image, it is resized twice, once to a more managable size, and once to a thumbnail, then, based on the number on images that user already has stored in a database, a number is put after the name.. so two images are saved like

jewbilee_1.jpg
jewbilee_1_thumb.jpg

then the database is written to say who uploaded it, the path to both pics.. and the caption, works perfectly.. ill remove the header though and add the confirmation

#16 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 11 April 2006 - 12:08 PM

having a new problem now.. with certain images, it keeps saying division by zero on the aspect ration part which leads me to believe it isnt actually getting the image, but it only does that (as far as i know now) with my prom pictures from this weekend, ive tested several large high quality images and it works fine.. but now with my prom ones..

<?
session_start();
include("connect.php");

$_SESSION['username'] = "$username";

echo "<br>Add a new image to your personal gallery<br>Only JPeg files may be uploaded as of now<br>";
$number = "SELECT * FROM gallery WHERE owner = '$username'";
$numberresult = mysql_query($number);
$number = mysql_num_rows($numberresult);
$number++;

if (isset($_FILES['userfile']['tmp_name'])){
$filename = $_FILES['userfile']['tmp_name'];

$destination_dir = "/usr/home/pwncae/public_html/gallery/";
$destination = $destination_dir.$username."_".$number.".jpg";
$destination_thumb = $destination_dir.$username."_".$number."_thumb.jpg";
$url = "http://www.pwncaeks.com/gallery/".$username."_".$number.".jpg";
$thumb = "http://www.pwncaeks.com/gallery/".$username."_".$number."_thumb.jpg";

$caption = $_POST['caption'];

$width = 650;
$height = 650;
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $destination, 100);

$width = 100;
$height = 100;
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $destination_thumb, 100);


$query = "INSERT INTO gallery (owner, url, thumb, caption) VALUES ('$username', '$url', '$thumb', '$caption')";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 


echo "Thumbnail image saved as $url<br>";
echo "<a href=\"$url\" target=\"new\"><img src=\"$thumb\"></a><br>$caption";


}else{
?>
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="POST">  
Image:<br><input name="userfile" type="file"><br>
Caption:<br><input type="text" name="caption" size="40"><br>
<input type="submit" name="upload" value="Upload">
<? }

?> 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users