FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Thumbnail Generation !





salman_500
hey guyz,

i finnaly got my gd library running and cant wait to get my own thumbnail generation script.... well here is exactly what need.....

The code that you supply has to be complete...

i use a form to upload an image ... form has field name "image" :

Code:
$maxfilesize = 1000000;
// check if there was a file uploaded
if (!is_uploaded_file($_FILES['image']['tmp_name'])) {
    $error = "<b>you didn't select a file to upload.</b><br />";
// if it was, go ahead with other checks
} else {
    if ($_FILES['image']['size'] > $maxfilesize) {
        $error = "<b>your image file was too large.</b><br />";
        unlink($_FILES['image']['tmp_name']);
    } else {
        $ext = strrchr($_FILES['image']['name'], ".");
        if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG") {
            $error = "your file was an unacceptable type.<br />";
            unlink($_FILES['image']['tmp_name']);
        // if it's there, an okay size and type, copy to server and update the photo value in SQL
        } else {
             
           $newname = $id.$ext;
           move_uploaded_file($_FILES['image']['tmp_name'],"modules/templates/images/".$newname);
           mysql_query("UPDATE templates SET image='$newname' WHERE id= '$id'") or die (mysql_error());
             
       }
   }
}


ok now what i want is a script that will use this same image to generate a thumbnail of it...

the thumbnail should :

width = 115 px
height = (in ratio to width.. but maximum of 140..clipping if excess)

ok now once it has been generated it has to be sent to a folder :

modules/templates/thumb/

the name of it has to be the same as the image... the name is randomly generated ... so you can use "$id" to get the same name for the thumb as the image itself... and then enter the value into the database using this code :

this will enter the same value as the image so no error occurs...

Code:
mysql_query("UPDATE templates SET thumb='$newname' WHERE id= '$id'") or die (mysql_error());


Note : The extention on the thumb has to be the same as the image. and that the code should be enough so that i can simply add to my existing page....

i currently use a manually uploading script to upload the thumb... the script you supply has to be so that i can simply replace my older script (very much similar to the above script) with this new one !

thnx !
JayBee
try PHPthumb
http://phpthumb.sourceforge.net/

it is quiet simple to integrate it to your website, it provide cache of resized images and many other features.
You can also look in source and find the part, that generates images.
I thing it can do everything you want
I use PHPthumb on all websites I have created.

Be carfule it can resize only small images!
calculating of the size is
- GD2 library want about 5Bytes per pixel
- in memory must be both images - original and thumbnail in same time
- php memlimit cause no image on output, when source image is so big

teoretical max image dimensions
Code:
8MB memlimit - 1492x1119px
16MB memlimit - 2112x1584px
salman_500
thnx for the link... but it has many extra features im not even concernec with.... i prefer a script that would only do what i mention in my first post...thnx !

any1 ready for this?

thnx in advance !
hexkid
salman_500 wrote:
i prefer a script that would only do what i mention in my first post

There's a good example you can adapt in the imagecopyresampled() manual page.
salman_500
Code:
<?php
// The file
$filename = 'solothemes.gif';

// Set a maximum height and width
$width = 115;
$height = 130;

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

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

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

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

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


ok i got this thing...i tried it and it works....shows the assigned size of the generated image......

but can you please add a feature to it the does no show it...but instead saves it to a folder :

modules/templates/thumb/

it should use this as its name ...

$id

this contains a randomly generated name for the file....

thnx !

P.S. im stillworking on solving the header problem... i dont know why but i get a headers already sent like this :

Quote:
Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\index.php:12)


and the lines near 12 are :

Code:
<?php require_once('Connections/conn.php'); ?>
<?php
session_start();
include "statistics/phphits.php";
phphitsAddHit();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<title>SoloThemes - The Best Free And Paid Web Templates !</title>
<link href="Templates/Urbane/CSS/css.css" rel="stylesheet" type="text/css">
\\ line 12 below this text !
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="top"><table width="70%"  border="0" cellspacing="0" cellpadding="0">
      <tr>


can you please look and see if there is a problem here.... thnx !!
hexkid
salman_500 wrote:
add a feature to it the does no show it...but instead saves it to a folder :

modules/templates/thumb/

it should use this as its name ...

$id

this contains a randomly generated name for the file....


Check the manual: http://php.net/imagegif

Code:
<?php

### ...

// Content type
### no header now, we're not sending the image to the client
#header('Content-type: image/gif');

### ...

// Output
### We don't want to output, we want to save it
#imagegif($image_p, null, 100);
### the imagegif() function only takes 2 parameters!!!
### instead of null, pass the filename as the 2nd parameter
imagegif($image_p, 'modules/templates/thumb/' . $id);
?>



salman_500 wrote:
P.S. im stillworking on solving the header problem...
Code:
<?php require_once('Connections/conn.php'); ?>
<?php
session_start();
// ...

Looks good Smile

You don't have anything except END-OF-LINE between "?>" and "<?php", right?
Code:
<?php require_once('...'); ?>[NO TABS OR SPACES HERE?]
<?php
And on lines before require_once()? Also no stray TABS or SPACES or BLANK LINES anywhere?
salman_500
hey,

that 2 modifications to the script actually do save the file to the folder !

its like what i wanted.. but to make it work better for me i wanna make it so that it accepts all basic extension of images... like .gif .jgp .jpeg .png

can you tell me how to do this?

thnx !

and about the header thing... i double checked my code and found that there are no stray spaces, tabs or any other stuff present... the text is just like that ..next line exactly after the end of the first line...

Crying or Very sad

any other thing that could bother this?
please gelp me with this.....

do you think this might be bothering it?

Code:
<!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->
hexkid
salman_500 wrote:
but to make it work better for me i wanna make it so that it accepts all basic extension of images... like .gif .jgp .jpeg .png

Code:
<?php
$extension = 'gif'; // or 'jpg', or 'png', ...

### ...

// Content type
switch ($extension) {
  case 'gif': header('Content-Type: image/gif'); break;
  case 'jpg': /* fallthrough */
  case 'jpeg': header('Content-Type: image/jpeg'); break;
  // ... other options
  default: exit('Invalid extension.'); break;
}

### ...

// Output
switch ($extension) {
  case 'gif': imagegif($image_p, null); break;
  case 'jpg': /* fallthrough */
  case 'jpeg': imagejpeg($image_p, null, 100); break;
  // ... other options
  default: exit('Script never reaches here. Previous default took care of it'); break;
}
?>



salman_500 wrote:
do you think this might be bothering it?

Code:
<!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->

YES! Can you move that to after the session_start() line?

Code:
<?php
session_start();
?>
<!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->
salman_500
hexkid wrote:
salman_500 wrote:
but to make it work better for me i wanna make it so that it accepts all basic extension of images... like .gif .jgp .jpeg .png

Code:
<?php
$extension = 'gif'; // or 'jpg', or 'png', ...

### ...

// Content type
switch ($extension) {
  case 'gif': header('Content-Type: image/gif'); break;
  case 'jpg': /* fallthrough */
  case 'jpeg': header('Content-Type: image/jpeg'); break;
  // ... other options
  default: exit('Invalid extension.'); break;
}

### ...

// Output
switch ($extension) {
  case 'gif': imagegif($image_p, null); break;
  case 'jpg': /* fallthrough */
  case 'jpeg': imagejpeg($image_p, null, 100); break;
  // ... other options
  default: exit('Script never reaches here. Previous default took care of it'); break;
}
?>



THNX !

but i wana ask somthin.... if im not to output an image... do i still want the headers and content type like you menstioned in the script above??? or is it another way to do t coz i wanted to save the image to a folder.... like you told me before...


oh and i tried to move the line to where u said i should...and i cant coz its a dreamweaver thingy...so i simply removed it from there.... but it still said that header were already sent... but from the line that had this code :

Code:
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">


so i removed all other stuff and made it inot this...

Code:
<body>


and when i refreshed the scriot..its still gave the errors and the line shifted to the next !!

Code:
<table width="100%"  border="0" cellspacing="0" cellpadding="0">


Crying or Very sad Embarassed Confused Mad Sad
salman_500
hey,

what do i use instead of this in order to get the extention of a random file??

Code:
$extension = 'gif'; // or 'jpg', or 'png', ...


coz i will be dealing with random images so ...so i need this script works as so that it can auto detect the extention...
thnx !
JayBee
PHP manual
Code:
$file_ext = substr($filename, strrpos($filename, '.') + 1); // returns the ext only



or use what you have already written Wink
salman_500 wrote:
Code:
$ext = strrchr($_FILES['image']['name'], ".");
        if ($ext != ".gif" AND $ext != ".jpg" AND $ext != ".jpeg" AND $ext != ".GIF" AND $ext != ".JPG" AND $ext != ".JPEG") {
}


or with this, you can test only lowercase extensions
Code:
$file_ext = strtolower(strrchr($filename, ".")));




You can just use only one extension, why to recognize the correct one. There is no reason to create thumbs with same extension like source image.
You want your code simple. So, don't do any difficulties.
All thumbs could be jpeg and you will have less work with generating and reading them.
salman_500
JayBee wrote:
PHP manual
Code:
<?php
substr($filename, strrpos($filename, '.') + 1); // returns the ext only
?>


You can just use only one extension, why to recognize the correct one. There is no reason to create thumbs with same extension like source image.
You want your code simple. So, don't do any difficulties.
All thumbs could be jpeg and you will have less work with generating and reading them.


thnx !!

and i do want the thumbs to be of the same extention as the images ..but what if the image is gif or jpg... that way i wont have to configure the script everytime a different image comes up...

thnx again !

and thank to hexkid too !! Very Happy Razz
JayBee
salman_500 wrote:
..but what if the image is gif or jpg... that way i wont have to configure the script everytime a different image comes up...


no
with imagecreatefromgd2 you read an image to memory and it is now bitmap
than you transform image and save it with function imagejpeg
with filename
Code:
$thumb_filename = $your_id.".jpg"

it's all

when you want to read your thumb, just use your ID again and return filename
Code:
$thumb_filename = $your_id.".jpg"


isn't it easy?
hexkid
JayBee: What if he has two (or more) different files with the same name except for the extension? Smile
Code:
test.jpg ==> thumbs/test.jpg
test.png ==> thumbs/test.png
test.gif ==> thumbs/test.gif


salman_500: Make sure session_start() is the very first thing in your script
Code:
<?php session_start(); ?>
<?php header('Content-Type: text/html; charset=iso-8859-1'); ?>
<!DOCTYPE ...>
<!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head><title>test</title></head>
<body>
...


One other option that I don't like is to use ob_start().
salman_500
hexkid wrote:

salman_500: Make sure session_start() is the very first thing in your script
Code:
<?php session_start(); ?>
<?php header('Content-Type: text/html; charset=iso-8859-1'); ?>
<!DOCTYPE ...>
<!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head><title>test</title></head>
<body>
...


One other option that I don't like is to use ob_start().


hey... moving session to first line didnt work... but replacing it with obstart() works !!!! Very Happy

THNX !!!

can you tell me why you dont like it... like is ther any disadvanteage or somthing?

thnx yet again ! Very Happy
hexkid
salman_500 wrote:
can you tell me why you dont like [ob_start()]... like is ther any disadvanteage or somthing?
Using ob_start() to avoid the "headers already sent" error indicates the script writer is lazy. Instead of doing it the proper way, (s)he resorts to ob_start().

It's somewhat like having the same username/password for all sites you go to. It works and it's easy to manage, but I hardly ever use the same password for different sites.


Or like installing all languages from a language pack with 130 languages when you really need only one but don't want to find which specific one you need.


It's like using a cannon to kill a fly Smile
JayBee
hexkid wrote:
JayBee: What if he has two (or more) different files with the same name except for the extension? Smile
Code:
test.jpg ==> thumbs/test.jpg
test.png ==> thumbs/test.png
test.gif ==> thumbs/test.gif



he has random numbers representing each uploaded file, so there couldn't be collision

for example
Code:
test.jpg ==> thumbs/91823749143.jpg
test.png ==> thumbs/9832472312.jpg
test.gif ==> thumbs/32423423234.jpg
hexkid
JayBee wrote:
he has random numbers ...
Oh! Right. I forgot about that.

What's it with random that "everybody" likes it so much?
PatTheGreat42
This is cool. I tried to make a thumbnail maker once, but it failed quite rapidly. I blame the fact that I spent about 15 minutes on it, tried to fix it only twice, and then went off to watch TV or something. Nice work.
salman_500
hexkid wrote:
salman_500 wrote:
can you tell me why you dont like [ob_start()]... like is ther any disadvanteage or somthing?
Using ob_start() to avoid the "headers already sent" error indicates the script writer is lazy. Instead of doing it the proper way, (s)he resorts to ob_start().

It's somewhat like having the same username/password for all sites you go to. It works and it's easy to manage, but I hardly ever use the same password for different sites.


Or like installing all languages from a language pack with 130 languages when you really need only one but don't want to find which specific one you need.


It's like using a cannon to kill a fly Smile


well if thats the only reason... ill ive with it ! Razz

by the way.. you know how much i tried to fix it... Very Happy Razz Laughing

anywayz... JayBee

i just want to have that option coz not all of the images i upload are of gif or png... so i want the thumbnail to be like them too.. i dont use gd lib for uploadin normal images...so they remain as the original image...and i want the thumb to be the same too Very Happy

thnx to all who helped me out here !!! Very Happy Surprised
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.