Jump to content

[SOLVED] Upload and watermark with a slight edit ?


Distant_storm

Recommended Posts

Ok ive made a captcha as just practice with image stuff. although I want now to be able to do the following...

 

1) Specify the temporary location for a upload file

2) not as much watermark the image but create image like a captcha making lines on it and some text

3) Destroy the temp image and the fully uploaded image

4) save the watermarked image.

 

Any ideas  on how id go about it .

Link to comment
Share on other sites

Ok ive made upload script uploads a file to a specific folder.

 

From that image now I want to watermark it although ive seen a watermark code which takes another image and merges it with the origonal image.

 

I don't want to do it this way. I want to use my captcha method of actully putting text onto the image and lines. although I don't know how to go about it because ive used functions to create the captcha from scratch rather than image. like so

 

function makeRGBColor($color,$image) {
$color = str_replace('#',"",$color);
$red = hexdec( substr($color,0,2) );
$green = hexdec( substr($color,2,2) );
$blue = hexdec( substr($color,4,2) );
$out = imagecolorallocate($image,$red,$green,$blue);
return($out);
}


$font="arial.ttf";
$fontsize=18;
$padding=18;


$wordbox= imageftbbox($fontsize,0,$font,$target);

$wordboxwidth=$wordbox[2];
$wordboxheight=$wordbox[1]; + abs($wordbox[7]);

$containerwidth = $wordboxwidth + ($padding *2);
$containerheight = $wordboxheight + ($padding * 2);

$textx= $padding;
$texty= $containerheight - $padding;

$captchaimage = imagecreate($containerwidth,$containerheight);

$backgroundcolor= makeRGBColor( 'EDEDED',$captchaimage);
$textcolor= makeRGBColor('A8ADB4', $captchaimage);

imagefttext($captchaimage,$fontsize,0,$textx,$texty,$textcolor,$font,$target);

$angle= rand(-20,20);
$captchaimage = imagerotate($captchaimage,$angle,$backgroundcolor);

$line = makeRGBColor('FFFFFF',$captchaimage);

for ($i = 0; $i < 3; $i++) {
$xstart= rand (0,$containerwidth);
$ystart= rand (0,$containerheight);
$xend= rand (0,$containerwidth);
$yend=rand (0,$containerheight);
imageline($captchaimage,$xstart,$ystart,$xend,$yend,$line);
}

header ('content-type:image/png');
imagepng($captchaimage);

 

 

Although i want this instead of on image box onto my image

 

I then want to save that image instead of it just being destroyed.

 

Any help would be much apreciated thanks

 

Link to comment
Share on other sites

If the image you want is a jpg, use ImageCreateFromJpeg();

If the image you want is a png, use ImageCreateFromPng();

If the image you want is a gif, use ImageCreateFromGif();

 

Then at the end to save it.

 

header (content-type: image/png)
ImagePng($captchaimage); //paints the image in the browser
ImagePng($captchaimage, "captcha.png"); //Saves it as a png file called 'captcha.png'
ImageDestroy($captchaimage); //clean up the resources

 

Hope that helps,

 

Sam

Link to comment
Share on other sites

Erm i Think ibroke it ?

 

This error

 

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 11392 bytes) in XXXX/XXXXX/XXXXXX/XXXXXX on line 83

 

What does that erorr mean ?

 

this is my code up to now...

 


$file_allocation= "../" . $base_url_temp . "/" . $_FILES['photofile']['name'];

//watermark image

function makeRGBColor($color,$image) {
$color = str_replace('#',"",$color);
$red = hexdec( substr($color,0,2) );
$green = hexdec( substr($color,2,2) );
$blue = hexdec( substr($color,4,2) );
$out = imagecolorallocate($image,$red,$green,$blue);
return($out);
}


$font="arial.ttf";
$fontsize=18;
$padding=18;

list($width, $height, $type, $attr) = getimagesize($file_allocation);

$textx= $padding;
$texty= $width - $padding;

$captchaimage = ImageCreateFromJpeg($file_allocation);

$backgroundcolor= makeRGBColor( 'EDEDED',$captchaimage);
$textcolor= makeRGBColor('A8ADB4', $captchaimage);

imagefttext($captchaimage,$fontsize,0,$textx,$texty,$textcolor,$font,"Kayleigh.co.uk");

$angle= rand(-20,20);
$captchaimage = imagerotate($captchaimage,$angle,$backgroundcolor);

$line = makeRGBColor('FFFFFF',$captchaimage);

for ($i = 0; $i < 3; $i++) {
$xstart= rand (0,$width);
$ystart= rand (0,$height);
$xend= rand (0,$width);
$yend=rand (0,$height);
imageline($captchaimage,$xstart,$ystart,$xend,$yend,$line);
}

header ('content-type: image/png');
ImagePng($captchaimage); 
ImagePng($captchaimage . "../" . $base_watermark . "/captcha.png"); 
ImageDestroy($captchaimage); 


 

any help will be much apreciated, I have never seen that error message before is the file just too big or something?

Link to comment
Share on other sites

'Tis a bit odd, since you don't have a line 83 :P

 

but there is a limit to file size, which you change in your php.ini, I think (feel free to correct me if I'm wrong).

 

Also, you have to make sure that the image you are making it from actually is .jpg, or .jpeg in order to use ImageCreateFromJpeg();

Link to comment
Share on other sites

I am pretty sure that my ini settings are still default which i believe is 8mb. I am testing image which are 726kb and they are jpg. I duno if they have to be non progressive ?

 

I have had some results which just show a box with red x, although i keep getting this error message

 

There is no line 83 because i haven't included all my code but i will..

 

 

<?php

//image upload handel

#Connect to database get file listing

if ($dbc = @mysql_connect('host','user','xxxxx')) {

if (@mysql_select_db('db')) {

$query="SELECT * FROM table";

if ($r = mysql_query ($query)) {

while ($row = mysql_fetch_array($r)) {

$base_url_temp=$row['xxx'];
$base_watermark=$row['xxxxx'];

}
}


// atempt to upload file

$allow_file="FALSE";
$allowed_file_types = array(1=>'image/gif',2=>'image/pjpeg',3=>'image/jpg',4=>'image/jpeg',5=>'image/JPG',6=>'image/JPEG',7=>'image/pjpg',8=>'image/png');
$total_file_types= count($allowed_file_types);

for($c=1;$c < $total_file_types; $c++) {

if ($allowed_file_types[$c] == $_FILES['photofile']['type']) {

$allow_file="TRUE";
}

}

if ($_FILES['photofile']['size'] > "8388608") {
$allow_file="FALSE";
}

if ($allow_file == "FALSE") {

exit("file was too big or wrong type" . $_FILES['photofile']['type'] . "<br>" . $_FILES['photofile']['size']);

}

if (move_uploaded_file ($_FILES['photofile']['tmp_name'],".." . $base_url_temp . "/" . $_FILES['photofile']['name'])) {

echo "file uploaded";

} else {

echo $_FILES['photofile']['error'];

}


$file_allocation= ".." . $base_url_temp . "/" . $_FILES['photofile']['name'];

//watermark image

function makeRGBColor($color,$image) {
$color = str_replace('#',"",$color);
$red = hexdec( substr($color,0,2) );
$green = hexdec( substr($color,2,2) );
$blue = hexdec( substr($color,4,2) );
$out = imagecolorallocate($image,$red,$green,$blue);
return($out);
}


$font="arial.ttf";
$fontsize=18;
$padding=18;

list($width, $height, $type, $attr) = getimagesize($file_allocation);

$textx= $padding;
$texty= $width - $padding;

$captchaimage = ImageCreateFromJpeg($file_allocation);

$textcolor= makeRGBColor('A8ADB4', $captchaimage);

imagefttext($captchaimage,$fontsize,0,$textx,$texty,$textcolor,$font,"Kayleigh.co.uk");

$line = makeRGBColor('FFFFFF',$captchaimage);

for ($i = 0; $i < 3; $i++) {
$xstart= rand (0,$width);
$ystart= rand (0,$height);
$xend= rand (0,$width);
$yend=rand (0,$height);
imageline($captchaimage,$xstart,$ystart,$xend,$yend,$line);
}


header ('content-type: image/gif');
ImagePng($captchaimage); 
ImagePng($captchaimage,"captcha.gif"); 
ImageDestroy($captchaimage); 



}
}

?>

Link to comment
Share on other sites

$file_allocation= ".." . $base_url_temp . "/" . $_FILES['photofile']['name'];


$captchaimage = ImageCreateFromJpeg($file_allocation); //line 83

 

So at the moment the $file_allocation is

(if we imagine $row['xxx']; = example, and $_FILES = file)

 

'..example/file', why is that? Or does $row['xxx'] include a "/"?

 

 

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.