PHP GD Library output an image and text content on same page

后端 未结 4 1200
失恋的感觉
失恋的感觉 2021-01-06 11:27

I am trying to output an image to a browser and then output HTML (not directly related to the image) on the same page. Is this possible? I am having a heck of a time figurin

4条回答
  •  长发绾君心
    2021-01-06 12:13

    Stop and think for a moment. How would you normally embed an image in a HTML file? You create two files: text.html and image.jpg. Same here, you will create two scrips, one that outputs the HTML and one that generates the image. The HTML would look like:

    generated image
    

    ross

    The generateimage.php script only generates the image.

    Lets take for example a form that allows the user to create a digital Christmas card: he can select the image and write a personal note beneath it.

    form.html:

    
    
    Select an image:
    Write a message:

    view_card.php:

    
      
        Here is your Christmas card:
        

    generateimage.php:

     'tree.jpg', 'santa' => 'santa.jpg');
    if( !isset($allowed_files[$_GET['imgname']])) {
        exit; // Thank you for playing...
    }
    
    /* Attempt to open */
    $im = @imagecreatefromjpeg($allowed_files[$_GET['imgname']]);
    
    /* See if it failed */
    if(!$im){
        /* Create a black image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
    
        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }
    
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
    ?>
    

提交回复
热议问题