Jump to content

saving a thumbnail


insrtsnhere13

Recommended Posts

[code]
<?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);
?> [/code]

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..
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

im using that now.. the image is created succesfully.. however, this "http://www.pwncaeks.com/dashboard/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
Link to comment
Share on other sites

[!--quoteo(post=363510:date=Apr 10 2006, 07:25 PM:name=insrtsnhere13)--][div class=\'quotetop\']QUOTE(insrtsnhere13 @ Apr 10 2006, 07:25 PM) [snapback]363510[/snapback][/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?
Link to comment
Share on other sites

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...

[code]
<?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">
<? }

?>
[/code]
Link to comment
Share on other sites

I took your code and ran it locally on my machine. With a few minor modifications it ran fine.[list][*]Remove this line: [code]<?php header('Content-type: image/jpeg'); ?>[/code] 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: [code]<?php imagejpeg($image_p, 'gallery/' . $_FILES['userfile']['name'], 100); ?>[/code][*]Make sure the directory where you're storing the thumbnail images is writable by the web server.[*]If you add this code:[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'] . '">';
?>[/code] right after storing the thumbnail image and before the "else", then you can see that it worked.[/list]
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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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..

[code]
<?
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">
<? }

?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.