Saving a Created Image to a Directory

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
zimick7
Forum Newbie
Posts: 17
Joined: Mon Dec 06, 2004 2:11 pm

Saving a Created Image to a Directory

Post by zimick7 »

I need help with a code that will create an image and then save it to a directory. I have worked with a code that will output it to a browser, but cannot figure out how to save it.

Code: Select all

<?php

// Create an image
$img = ImageCreate(100,100);
$black = ImageColorAllocate($img,0,0,0);

ImageFill($img,0,0,$black);

ImageDestroy($img);

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

might want to take a look at [php_man]copy[/php_man]
zimick7
Forum Newbie
Posts: 17
Joined: Mon Dec 06, 2004 2:11 pm

Post by zimick7 »

that makes sense! Thanks!
zimick7
Forum Newbie
Posts: 17
Joined: Mon Dec 06, 2004 2:11 pm

Post by zimick7 »

this is the code I ended up using... this allows for quality control on the images...

Code: Select all

<?php
$img = ImageCreate(100,100);
$black = ImageColorAllocate($img,0,0,0);

ImageFill($img,0,0,$black);


$path = '/path/to/dir/image.jpg';
imageJpeg($img,$path,100);


ImageDestroy($img);

?>

Do I still need to use ImageDestroy at the end if I am not outputting it to the browser?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It is always good practice to clean up after your code :). BTW-- You might want to also take a look at [php_man]chmod[/php_man] to set permissions on the fly when creating images. Having your permissions change when need be will save you a lot of time when you want scalability of your script.
zimick7
Forum Newbie
Posts: 17
Joined: Mon Dec 06, 2004 2:11 pm

Post by zimick7 »

thanks for the tips! much appreciated!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I would consider changing the image quality from 100 to about 80, as you cant really tell the difference and the files sizes are MUCH smaller.
Post Reply