Use Mason and PHP to generate an image plug-in

Source: Internet
Author: User

Fanwe's image plug-in can be used for forums and blogs to present user information and some latest news in the form of images.

If you need to develop this function on your own, you need to set images in a certain format (or address) to use scripts for processing.

In Perl

For example, you can use the locationmatch or filesmatch command of Apache to set up a Perl module for processing.

If you use Mason, it is simpler. You do not need to modify the server. You only need to use the dhandler feature to let the specified script handle it.

The basic process is as follows:

The main code is as follows:

My $ uri = $ R-> URI; </P> <p> if ($ uri = ~ /. JPG $/) {<br/> $ uid = substr ($ uid, 1,-4 ); # obtain uid from URI </P> <p> my $ image_template_path = $ M-> callers (0)-> source_dir. '/images/'; # background image folder <br/> my $ username = User: GetUserName ($ UID); </P> <p> $ counter = imagecreate :: create ($ total, $ username, $ image_template_path); # create an image <br/> $ R-> content_type ('image/jpg '); <br/> $ R-> headers_out; <br/> Print $ counter; <br/>}

PHP is similar. For example, you can set it in Apache or in the. htaccess file:

<Directory/var/www/php.hfahe.cn> <br/> redirectmatch ^/(. *)/. jpg $/avatar. php? Type = $1 <br/> </directory> 

The PHP code is as follows:

Include ('lib/Conn. PHP '); </P> <p> // obtain the user ID from the parameter <br/> $ input_param = $ _ Get ["type"]; <br/> preg_match_all ("| r (. *) _ | u ", $ input_param, $ preg_out ); </P> <p> // obtain the user nickname from the database <br/> $ SQL _string = "select username from user where id = ". $ preg_out [1] [0]; </P> <p> $ result = mysql_db_query ($ mysql_database, $ SQL _string, $ conn ); </P> <p> $ ROW = mysql_fetch_row ($ result); </P> <p> $ user_nickname = $ row [0]; </P> <p> header ("Content-Type: image/JPEG "); </P> <p> // create an image with a width of 119 and a height of 126 pixels <br/> $ background_image = imagecreatetruecolor (119,126 ); </P> <p> // obtain the text color, RGB <br/> $ background_color = imagecolorallocate ($ background_image, 250,228,130 ); </P> <p> // read our background image <br/> $ source_image = imagecreatefrompng ('images/u0_2.png '); </P> <p> // copy the background image to the image to be output. <br/> imagecopy ($ background_image, $ source_image, 0, 0, 0, 0,119,126); </P> <p> // use a font that supports Chinese characters <br/> $ text_font = '/usr/share/fonts/Vista/msyh. ttf'; <br/> $ text_size = 10; </P> <p> // write the user nickname to the image. <br/> imagettftext ($ background_image, $ text_size, 0, 8, 40, $ background_color, $ text_font, $ user_nickname); </P> <p> // output PNG image content <br/> imagejpeg ($ background_image ); </P> <p> // disable the previously used image buffer <br/> imagedestroy ($ background_image); <br/> imagedestroy ($ simage ); 

Then, we can see the image we need when calling r23_0.jpg.

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.