Jump to content

Save image from imagecreatefromjpg


Mateobus

Recommended Posts

Hey all,

I am trying to implement a captcha on my website. Rather than creating the image on the fly, I would like to have a database of the captcha codes, and a thousand or so associated images.  I was wondering if anyone knew how to save an image to a directory on the server, after it has been created with the imagecreatefrompng or imagecreatefromjpg function.  Here is the code i found from a tutorial

 

<?php
/*
Now for the GD stuff, for ease of use lets create
the image from a background image.
*/

$captcha = imagecreatefrompng("./captcha.png");

/*
Lets set the colours, the colour $line is used to generate lines.
Using a blue misty colours. The colour codes are in RGB
*/

$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);

/*
Now to make it a little bit harder for any bots to break, 
assuming they can break it so far. Lets add some lines
in (static lines) to attempt to make the bots life a little harder
*/
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
?> 

 

so, i was planning on putting that in a loop and saving the images in a folder.  Maybe not the most secure way, but its the most efficient. Does anyone know how to move that file to a non-temporary directory on the server?

Link to comment
Share on other sites

Mateobus,

The write out function would be imagepng() .

http://us2.php.net/manual/en/function.imagepng.php

 

So right where you've left off (or in your loop) ...

 

// path to save, dir must be 777 , you are web-user not "you"
$captcha_path = '/home/you/www/project/path/captcha/'.

//save the file
imagepng($captcha , $captcha_path.'filename.png');

 

imagepng() also be used with headers (see php.net examples) to send an image like this..

<img src="captcha.php">

, but there can be no other output.

 

If you do decide to premake images (and I don't think that you should) , get friendly also with opendir() , readdir() , and the holy file_exists() .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.