警告:imagedestroy()期望参数1为资源

时间:2019-01-12 03:32:09

标签: php imagick

我正在尝试创建用于生成验证码的代码,这是我的代码的第一部分:

    <?php

    $flag = 5;

    if (isset($_POST["flag"])) //  check that POST variable is not empty
    {
        $input = $_POST["input"];
        $flag = $_POST["flag"];
    }

    if ($flag == 1) // submit has been clicked
    {
        if (isset($_SESSION['captcha_string']) && $input == $_SESSION['captcha_string']) // user input and captcha string are same
        {

            ?>

            <div style="text-align:center;">
                <h1>Your answer is correct!</h1>

                <form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> // refresh the page
                    <input type="submit" value="refresh the page">
                </form>
            </div>

    <?php

        } else // incorrect answer, captcha shown again
        {

            ?>
            <div style="text-align:center;">
                <h1>Your answer is incorrect!<br>please try again </h1>
            </div>
            <?php
            //!!!!!!13. On refresh of CAPTCHA or web site destroy the CAPTCHA. Delete “CAPTCHA.jpeg” file.
            imagedestroy('C:\xampp\htdocs\CAPTCHA2\Images\CAPTCHA.jpg');
            create_image();
            display();
        }

} else // page has just been loaded
{
    create_image();
    display();
}

我做了一些研究,但我似乎不知道问题出在哪里。这是我尝试输入验证码时出现的警告:

  

注意:未定义索引:在第33行的C:\ xampp \ htdocs \ CAPTCHA2 \ latest.php中输入

     

警告:imagedestroy()希望参数1为资源,在第63行的C:\ xampp \ htdocs \ CAPTCHA2 \ latest.php中给出的字符串

下面是用于创建验证码图像的功能

    function  create_image()
{
    $fonts = array( dirname(__FILE__) . '/Fonts/arial.ttf',  dirname(__FILE__) . '/Fonts/consolaz.ttf',  dirname(__FILE__) . '/Fonts/kenyc.ttf');
    $word = '';
    $w = 280;
    $h = 120;
    $matrix_c = 240/3;
    $matrix_h = 110/3;

    //!!!!!!!2. Create 2 dimensional w*h rectangle.
    $image2 = imagecreatetruecolor($w, $h); 
    $background_color = imagecolorallocate($image2, 255, 255, 255);  
    imagefilledrectangle($image2,0,0,$w,$h,$background_color);


    //!!!!!!!3. Add transparent background in rectangle. With w width and h height so its fit to rectangle.
    imagealphablending($image2, false); 
    $transparency = imagecolorallocatealpha($image2, 0, 0, 0, 127);
    imagefill($image2, 0, 0, $transparency);
    imagesavealpha($image2, true);


    //!!!!!!!4. Take random letter and number from "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" and generate one n number String.

    //NOTE: But for selecting letters avoid some same type of letters so its not make any confusion to users. Like I/1, 0/O, Q/O, C/G, h/b After removing this letters, select string only from "acdefgijklmnopqrstuvwxyzABCDEFHJKLMNPRSTUVWXYZ23456789"

    $R = rand(0,255); 
    $G = rand(0,255); 
    $B = rand(0,255); 

    $letters = 'ABCYZ2349';
    //$letters = 'acdefgijklmnopqrstuvwxyzABCDEFHJKLMNPRSTUVWXYZ23456789';
    $len = strlen($letters);
    $letter = $letters[rand(0, $len-1)];
    $text_color = imagecolorallocate($image2, $R,$G,$B);

    //!!!!!!!5. That random String put in rectangle on transparent background. Make this string font color black only.
    for ($i = 0; $i<6;$i++) {
        $letter = $letters[rand(0, $len-1)];
        $R = rand(0,255); 
        $G = rand(0,255); 
        $B = rand(0,255); 
        imagettftext($image2, 38,  GetGrad(), 20+($i*40), 75, imagecolorallocate($image2, $R,$G,$B), $fonts[rand(0,2)], $letter);
        $word.=$letter;
    }

    //!!!!!!!6. Put this rectangle in session
    $_SESSION['captcha_string'] = $word;

    $out = imagecrop($image2,['x' => 10, 'y' => 25, 'width' => 240, 'height' => 60]);
    $line_color = imagecolorallocate($image2, 255, 255, 255); 
    imagesetthickness ($image2,1);

    for ($i = 0; $i<0;$i++) {
        imageline($image2,0,$matrix_h,280,$matrix_h,$line_color);
        imageline($image2,0,$matrix_h*2,280,$matrix_h*2,$line_color);
        imageline($image2,$matrix_c,0,$matrix_c,120,$line_color);
        imageline($image2,$matrix_c*2,0,$matrix_c*2,120,$line_color);
    }

    $images = glob("*.png");
    foreach($images as $image_to_delete)
    {
        unlink($image_to_delete);      
    }

    //!!!!!!!7. Now give random rotation on that all letters and re-arrange in rectangle.
    imagepng($image2, 'C:\xampp\htdocs\CAPTCHA2\Images\image2.png');
    $image = new imagick( 'C:\xampp\htdocs\CAPTCHA2\Images\image2.png' );
    $distort = array( 10,0,10,0, 0,100, 20,100, 10,250, 20,200,
    253,240, 133,190, 266,400, 266,400, 266,100, 246,100,
    266,10, 266,0, 133,0, 153,20 );
    $image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
    $image->distortImage( Imagick::DISTORTION_SHEPARDS, $distort, true  );


    $image->writeImage('C:\xampp\htdocs\CAPTCHA2\Images\image2.png');

    //!!!!!!!8. Now divide rectangle in r*c matrix.
    imageline($image2,0,$matrix_h,280,$matrix_h,$line_color);
    imageline($image2,0,$matrix_h*2,280,$matrix_h*2,$line_color);
    imageline($image2,$matrix_c,0,$matrix_c,120,$line_color);
    imageline($image2,$matrix_c*2,0,$matrix_c*2,120,$line_color);

    //!!!!!!!10. Insert one background that consist black color’s n number of lines.
    $png = imagecreatefrompng('C:\xampp\htdocs\CAPTCHA2\Images\image2.png');
    $jpeg = imagecreatefromjpeg('C:\xampp\htdocs\CAPTCHA2\Images\bg2.jpg');

    $out = imagecreatetruecolor($w, $h);

    //!!!!!!!11. Insert this background in Rectangle
    imagecopyresampled($out, $jpeg, 0, 0, 0, 0, $w, $h, $w, $h);
    imagecopyresampled($out, $png, 0, 0, 0, 0, $w, $h, $w, $h);



    $out = imagecrop($out,['x' => 10, 'y' => 25, 'width' => 240, 'height' => 60]);


    //!!!!!!!12.Export as jpeg file name: “CAPTCHA.jpeg”
    imagejpeg($out, 'C:\xampp\htdocs\CAPTCHA2\Images\CAPTCHA.jpg');

}

我最近才刚开始学习php,非常感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

在阅读imagedestroy(http://php.net/manual/en/function.imagedestroy.php)的规范后,似乎imagedestroy的参数指定了由imagecreatetruecolor生成的对象。 我认为从create_image()返回$ image2不会发生警告。

$image2 = create_image();
imagedestroy($image2);

但是,据我所知,您似乎想从文件系统中删除CAPTCHA.jpg,如何将imagedestroy更改为取消链接?

unlink ('C:\xampp\htdocs\CAPTCHA2\Images\CAPTCHA.jpg');