473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

php GD makeing thumbnails to a certain size

heya guys, im trying to take a variable input file and make a thumbnail so my gallery pageloads are lower. i want all the thumbnails to end up around 15ish kb, i know my current settings dont display that - im still testing.
it works on smaller files 250k, resizes to about 20ish k and 170k to about 3k, but i cant get it to resize bigger images and the while loop wont stop when the image reaches a certain size, i dont know where to go... pls help me debug this bugger ;)

Expand|Select|Wrap|Line Numbers
  1. $target_size=90000; // 7.5KB
  2. $initial_size=$target_path.$newfile;
  3. $filesize=filesize($initial_size);
  4. $file=$target_path.$newfile;
  5. $x=0;
  6. $reduction=($filesize*5)/100;
  7. $size = 0.9;
  8. while($filesize>$target_size){
  9.     $x++;
  10.     $save = $target_path."thumb_".$newname.".jpg";
  11.     $file = $file;
  12.     header('Content-type: image/jpeg') ; 
  13.     list($width, $height) = getimagesize($file) ; 
  14.     $modwidth = $width * $size; 
  15.     $modheight = $height * $size; 
  16.     $tn = imagecreatetruecolor($modwidth, $modheight) ; 
  17.     $image = imagecreatefromjpeg($file) ; 
  18.     imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
  19.     imagejpeg($tn, $save, 90) ;
  20.     $file = $target_path."thumb_".$newname.".jpg"; 
  21.     $filesize=$filesize-$reduction;
  22.     $size=$size-0.05;
  23.     }
  24. echo "resized $x times.";
Apr 27 '07 #1
1 1436
pbmods
5,821 Expert 4TB
In a situation like this, I like to write all my variables to a file so I can keep track of what's going on.

Basically, you want to take advantage of the fwrite function. fwrite will write to the file directly and immediately so you don't have to worry about whether you will see output if you stop the browser from trying to load the page.

Naturally, you'll probably want to eventually write your own Debugger object, but for now, try something like this:

[php]
$reduction=($filesize*5)/100;
$size = 0.9;

// [DBuG]
$debugFile = fopen('/path/to/debug/file.txt');

while($filesize>$target_size){
$x++;
.
.
.

// // [DBuG]
fwrite($debugFile, <<<EOF
AFTER RUN #$x:
\$filesize = $filesize
(etc.)


EOF;

$size=$size-0.05;
}
echo "resized $x times.";


// [DBuG]
fclose($debugFile);
[/php]

After you're pretty sure your loop is definitely infinite, stop your browser (and kill the php process), then open up your debug file and take a look at what wasn't changing.

Of course, this won't fix your problem, but it should help give you a better idea of exactly what is going on.
Apr 28 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Lemming | last post by:
Hi, I'm putting together a website for a local estate agent. One of the things they want to do is upload property descriptions/prices/etc. plus a picture of the property which is for sale.
5
by: Ken | last post by:
I am in the process of designing my first web-site, and am having a problem with my picture gallery. My thumbnails are all different sizes, I would like them to be one size. The manual does not...
3
by: Vagabond Software | last post by:
I'm trying to display thumbnail images in a Listview that look more like the Windows thumbnail view. Everything is working pretty good, but my thumbnails are decidedly not like the Windows...
6
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
0
by: rose.kevin | last post by:
Hi I am trying to get Windows Explorer to display thumbnails for my own custom filetype. I have found plenty examples out there telling me how to extract thumbnails from explorer for your own...
2
by: Krustov | last post by:
The question of how to create thumbnails with clickable links gets asked on this newsgroup every so often - like clock work . Just really posing this script here for google to archive it & so...
4
by: Barely Audible | last post by:
Guys I was wondering if it was possible to have a js file that, when the page loaded, it automatically generated a a set of thumbnails from directory of the page on the web server? Or would...
5
by: JJ | last post by:
I have a gallery-like application. (The gallery will be actually presented in Flash, but the management (cms) of the images will be in asp.net. ) My question is, is it ok to create Thumbnail...
2
by: GK | last post by:
Hi all, I wish to know , how i can create thumbnails automatically using javascripts. It should reduce the size as well as pixel resolution (ie, a 1 mb image of 1280 x 800 px can be reduced to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.