Mass Image Creation Script Troubles.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Mass Image Creation Script Troubles.

Post by Greenconure »

Explanation of where I am:
I have written a script that I hoped would accomplish the task of making a large amount (a little more than 700) individual .pngs from a database. The database has four fields, the name, the "r" value, the "g" value, and the "b" blue value. When I run the script with a test table that is identical in setup to the master table with the 700 rows except that it has only 1 row, it works perfectly. When I run the script with the large table, it creates lots of files with the correct names, that aren't images. They appear to be shortcuts of some kind.
(See the attached image to for a screenshot of the files this script creates)
My Questions:
  • Obviously I need some help, but besides the "Halp meh!" cry, I was wondering...
  • Am I trying to perform a task that is too demanding for the server?!
  • Is there a better way of doing this?
The Code
The four asterisks found throughout the script ("****") symbolize a value or variable that I took out for my own security.

Code: Select all

<?php
    $host = '****';
    $user = '****';
    $pass = '****';
    $db   = '****';
 
$link = mysql_connect($host, $user, $pass)
    or die('Could not connect: ' . mysql_error());
mysql_select_db($db) or die('Could not select database');
 
$query = 'SELECT * FROM **** ';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
while ($row = mysql_fetch_assoc($result)) {
        $string = $row['name'];
        $saveName = $row['name'];
        $r = $row['r'];
        $g = $row['g'];
        $b = $row['b'];
    
        $im    = imagecreate(180,36);
        $color = imagecolorallocate($im, $r, $g, $b);
        $black = imagecolorallocate($im, 0, 0, 0);
        $white = imagecolorallocate($im, 255, 255, 255);
        $name  = $row['name'];
        $fName = $name . ".png";
 
        imagefilledrectangle($im,0,0,180,36, $white); //BG
        imagefilledrectangle($im,1,1,118,34, $black); //Rectangle - Border
        imagefilledrectangle($im,2,2,117,33, $color); //Rectangle - Fill
        imagestring($im,5,122,8,$name,$black); //Text
        imagepng($im, $fName);
        imagedestroy($im);
}
 
mysql_free_result($result);
mysql_close($link);
?>
Attachments
Screenshot of the file browser in Coda showing the files created.
Screenshot of the file browser in Coda showing the files created.
Picture 4.png (13.58 KiB) Viewed 1314 times
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Mass Image Creation Script Troubles.

Post by Greenconure »

Sorry for the bump, but this is extremely important to me.
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: Mass Image Creation Script Troubles.

Post by lonelywolf »

have you set limit time ?

Code: Select all

set_time_limit(0);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Mass Image Creation Script Troubles.

Post by John Cartwright »

Greenconure wrote:Sorry for the bump, but this is extremely important to me.
As unfortunate as it is, your importance is not ours :) Please keep your bumping to a minimum of 24h. Thanks.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Mass Image Creation Script Troubles.

Post by onion2k »

Try unsetting $im after you call imagedestroy(). eg unset($im); ... By the looks of things the first file is being created ok, so there could be some remnant of the previous image messing up the next one. I'm not sure though...
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Mass Image Creation Script Troubles.

Post by Greenconure »

Jcart wrote:
Greenconure wrote:Sorry for the bump, but this is extremely important to me.
As unfortunate as it is, your importance is not ours :) Please keep your bumping to a minimum of 24h. Thanks.
Sorry about that & it won't happen again. I thought I had read the rules thoroughly but… :|
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Mass Image Creation Script Troubles.

Post by Greenconure »

onion2k wrote:Try unsetting $im after you call imagedestroy(). eg unset($im); ... By the looks of things the first file is being created ok, so there could be some remnant of the previous image messing up the next one. I'm not sure though...
I tried that… but no cigar. I'm going to see if adding a time limit will help.
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Mass Image Creation Script Troubles.

Post by Greenconure »

I modified the code so that it only "ran" using two rows in the table..

Code: Select all

$query = 'SELECT * FROM `table` LIMIT 0, 2';
And that produced
  • One good image
  • A "Shortcut" to the other image (It doesn't work)
  • A "Shortcut" to "img5" (Which is very odd, because I don't have a "img5" in my table or even database!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Mass Image Creation Script Troubles.

Post by onion2k »

Have you commented out the image creation code and tried echo'ing out the database content to make sure it is what it should be? I really can't see anything wrong with that code.
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Mass Image Creation Script Troubles.

Post by Greenconure »

onion2k wrote:Have you commented out the image creation code and tried echo'ing out the database content to make sure it is what it should be? I really can't see anything wrong with that code.
It echo's the information perfectly.
What I find odd is that even if I have the script run with two rows, it still creates the "shortcuts" and not images - so I'm pretty sure I'm not running a task that would be too hard for the server.
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Mass Image Creation Script Troubles.

Post by Greenconure »

Jcart wrote:
Greenconure wrote:Sorry for the bump, but this is extremely important to me.
As unfortunate as it is, your importance is not ours :) Please keep your bumping to a minimum of 24h. Thanks.
This time, I knew to wait :]
Post Reply