Several questions about PHP generated images

Source: Internet
Author: User
Tags color identifier image identifier transparent color

The first question that I encountered earlier was when I generated a text verification code using PHP, the general tip:

Warning:imagettftext (): Could not find/open font in/var/www/html/touming.php on line 11

Or

The image "http://localhost/img.php" cannot be displayed because it contains errors.

The source code is as follows:

<?php    //1. Generate a true color map    $img = Imagecreatetruecolor ($);    2. Color    $color =imagecolorallocate ($img, 255,255,255);    3. Set    the transparent imagecolortransparent ($img, $color);    Imagefill ($img, 0,0, $color);    4. Write to the canvas    $textcolor =imagecolorallocate ($img, 0,0,0);    Imagettftext ($img, 0, ten, $textcolor, "MYFONTS/SIMSUN.TTC", "Test");    5. Save    The header ("Content-type:image/png");      Imagepng ($img);    6. Release of    Imagedestroy ($img); >

The internet did not find the reason, the back is because the Apache process is not authorized to read the MyFonts folder problem, the current

The owner and group of the myfonts file are not Apache, so try to change owner and group to Apache:

sudo chown-r Apache myfonts

sudo chgrp-r Apache myfonts

Results The program executes successfully.

A second question

It is very early to know that there is a picture is no background, there is no object in the picture of the place is hollow, no background, not

The background is white, in some image browser PS can see the gray and white squares, such as the following picture,

I do not know what the name of this picture, but also do not know how PHP should generate this image, today found a blog post:

Blog.sina.com.cn/s/blog_7196ad2d0100qy0u.html

It's a good solution to my problem. The following is the original blog:

---------------------------------------------------------------------------------------------------------------

Recent projects, the front desk with a lot of transparent background text pictures, and the text is also used in two kinds of fonts in English mix,

Requires background to automatically generate the corresponding image according to user input. Previously, all the pictures used were made of PS,

This time can ... No way, had to bite the bullet to read the PHP GD document ... Finally......

The image processing slice of the PHP document finds such a function imagecolortransparent

(PHP 3, PHP 4, PHP 5)

Imagecolortransparent--Defines a color as a transparent color

Description

int imagecolortransparent (resource image [, int color])

imagecolortransparent () image sets the transparent color in the image to color . imageis the image identifier returned by Imagecreatetruecolor () , which color is the color identifier returned by Imagecolorallocate () .

Note: transparent color is an attribute of an image, and transparency is not a property of a color. Once you have set a color as a transparent color, any areas of the image that were previously drawn as that color become transparent.

Returns the identifier of the new transparent color, color or the identifier of the current transparent color if omitted.

Note: transparency can only be copied through Imagecopymerge () and true color images, not with imagecopy () or palette images.

So the rest is going to be the same.

1. Generate a true Color map

$img = Imagecreatetruecolor (200, 200);

2. Coloring

$color =imagecolorallocate ($img, 255,255,255);

3. Set the transparency

Imagecolortransparent ($img, $color);

Imagefill ($img, 0,0, $color);

4. Write to the canvas

$textcolor =imagecolorallocate ($im, 0,0,0);

Imagettftext ($img, 0, ten, $textcolor, "SIMSUN.TTC", "Test");

5. Save

Imagepng ($img, "aaa.png");

6. Release

Imagedestroy ($IMG);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.