简体   繁体   中英

How to implement php imagecreatetruecolor from php 5 to php 7

I have one captcha creator with imagecreatetruecolor, when i was using php 5 this works, but now in php 7 doenst. I haven't made any changes in the file, and i cant find any difference with the documentation.

I've tried that in chrome, edge and firefox, in any navigator works.

here is my code.

#create image and set background color
$captcha = imagecreatetruecolor(120,35);
$color = rand(128,160);
$background_color = imagecolorallocate($captcha, $color, $color, $color);
imagefill($captcha, 0, 0, $background_color);

#draw some lines
for($i=0;$i<10;$i++){
    $color = rand(48,96);
    imageline($captcha, rand(0,130),rand(0,35), rand(0,130), rand(0,35),imagecolorallocate($captcha, $color, $color, $color));
}

#generate a random string of 5 characters
$string = substr(md5(rand()*time()),0,5);

#make string uppercase and replace "O" and "0" to avoid mistakes
$string = strtoupper($string);
$string = str_replace("O","B", $string);
$string = str_replace("0","C", $string);

#save string in session "captcha" key
session_start();
$_SESSION["captcha"]=$string;

#place each character in a random position
putenv('GDFONTPATH=' . realpath('.'));
$font = 'arial.ttf';
for($i=0;$i<5;$i++){
    $color = rand(0,32);
    if(file_exists($font)){
        $x=4+$i*23+rand(0,6);
        $y=rand(18,28);
        imagettftext  ($captcha, 15, rand(-25,25), $x, $y, imagecolorallocate($captcha, $color, $color, $color), $font, $string[$i]);
    }else{
        $x=5+$i*24+rand(0,6);
        $y=rand(1,18);
        imagestring($captcha, 5, $x, $y, $string[$i], imagecolorallocate($captcha, $color, $color, $color));
    }
}

#applies distorsion to image
$matrix = array(array(1, 1, 1), array(1.0, 7, 1.0), array(1, 1, 1));
imageconvolution($captcha, $matrix, 16, 32);

#avoids catching
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); 

#return the image
header("Content-type: image/jpeg");
imagejpeg($captcha);

this is the output:

输出

in a dedicated tab:

专用标签中的验证码

Thanks for all, when i removed the header("Content-type: image/jpeg"); a warning was displayed, there was an error calling the font of the captcha.

i changed:

$font = 'arial.ttf';

for:

$font = '/arial.ttf';

then it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM