/    Sign up×
Community /Pin to ProfileBookmark

Saving a dynamic image

Hi,

I’ve wrote a simple script that opens an image file changes it a bit and then displays it in the browser window. These are the basics

[code=php]
<?php

Header(“Content-Type: image/png”);

$myimage = ImageCreateFromPng(“image.png”);

// do something ramdom to it

ImagePng($myimage);

ImageDestroy($myimage);

?>
[/code]

Instead of having this script output directly to the browser, I would like it to save the image to the server and have the html point to the static output.

Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelAug 24.2005 — Something like this:
[code=php]<?php

ob_start();

$filename = 'png' . mktime() . '.png';

$myimage = imagecreatefrompng('image.png');

// do something ramdom to it

imagepng($myimage);

$imagevariable = ob_get_contents();

ob_end_clean();

if (!$handle = fopen($filename, 'x'))
{
echo 'Cannot open file (' . $filename . ')';
exit();
}
if (fwrite($handle, $imagevariable) === FALSE) {
echo 'Cannot write to file (' . $filename . ')';
exit;
}
echo 'Image has been saved.';
fclose($handle);

imagedestroy($myimage);

?>[/code]
Copy linkTweet thisAlerts:
@MauAug 24.2005 — A more effecient way:

[code=php]$filename = 'png' . mktime() . '.png';

$myimage = imagecreatefrompng('image.png');

// do something ramdom to it

imagepng($myimage,$filename); // saving to $filename
imagedestroy($myimage);
[/code]
Copy linkTweet thisAlerts:
@mintauthorAug 24.2005 — The scripts work a treat, thank you both.
×

Success!

Help @mint spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...