Page 1 of 1

Saving a Created Image to a Directory

Posted: Mon Dec 13, 2004 9:50 pm
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);

?>

Posted: Mon Dec 13, 2004 10:06 pm
by John Cartwright
might want to take a look at [php_man]copy[/php_man]

Posted: Mon Dec 13, 2004 10:09 pm
by zimick7
that makes sense! Thanks!

Posted: Mon Dec 13, 2004 10:11 pm
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?

Posted: Mon Dec 13, 2004 10:28 pm
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.

Posted: Mon Dec 13, 2004 11:26 pm
by zimick7
thanks for the tips! much appreciated!

Posted: Tue Dec 14, 2004 12:14 am
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.