简体   繁体   中英

How to add image to our site with extension .php

Sorry if it's stupid question. I seeing on php.net can do like this. 这个

I do like this

$imagepath="smile.jpg";

$image=imagecreatefromjpeg($imagepath);
$imgheight=imagesy($image);
$color=imagecolorallocate($image,0, 153, 51);
imagestring($image, 20, 50, $imgheight-30, "this is testing", $color);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

My code can show image on browser and when I element to inspect it's show it's called in extensions .php right as i wish but I can not add text or style or anything on it.

For example, I just add more code like this

<div style="height: 600px;width: 100%; position: relative; border:1px solid red">
<div>
    <?php echo "Hello text"; ?>
</div>
<div style=" float: left;height: 600px;margin: 0 auto;border:1px solid yellow">
    <?php
        $imagepath="smile.jpg";
        $image=imagecreatefromjpeg($imagepath);
        $imgheight=imagesy($image);
        $color=imagecolorallocate($image,0, 153, 51);
        imagestring($image, 20, 50, $imgheight-30, "this is testing", $color);
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
    ?>
</div>
<div style="clear:both"></div>

it's show 在此处输入图片说明

anyone can help me this problem

thanks

you have to put php code into another file and add an exit instruction and put header instruction at begin like this :

header('Content-Type: image/jpeg'); //Before every instruction
$imagepath="smile.jpg";

$image=imagecreatefromjpeg($imagepath);
$imgheight=imagesy($image);
$color=imagecolorallocate($image,0, 153, 51);
imagestring($image, 20, 50, $imgheight-30, "this is testing", $color);

imagejpeg($image);
imagedestroy($image);

exit; // You have to add exit instruction to escape rendered attempt 

The simplest way to serve an image from PHP is this. This will work but you won't be able to make any changes to the image within the code itself.

However this would allow you to carry out other actions on your server, eg logging to a file or database that the image was viewed.

$image = file_get_contents('/path/to/image/.jpg');
header('Content-type: image/jpg');
echo $image;
die;

The problem that your image code does not work inlined in the html is because of the header

header('Content-Type: image/jpeg');

You can only set headers before any content is served (in your case you echo html before the header() which makes it fail. Refering to an image rendered by php can only be done by using a separate php file for the image and refer to the file with <img src="file.php"> .

I understand that you want to add some text as well, but the text is not part of an image and cannot be served using these headers. For the text to be in or below the image you need to alter the image itself.

To edit images using php check the page here for some usefull functions: http://www.php.net/manual/en/book.image.php

Hope that helps.

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