Image generator-PHP source code

Source: Internet
Author: User
Tags imagejpeg
Image generator image generator

Width = $ width; $ this-> Height = $ height; $ this-> BackColor = $ backColor; $ this-> _ image = imagecreatetruecolor ($ this-> Width, $ this-> Height); if (headers_sent () {throw new RuntimeException ('header sent. ');} // Initialization background $ backColor = imagecolorallocate ($ this-> _ image, (int) ($ this-> BackColor % 0x1000000/0x10000), (int) ($ this-> BackColor % 0x10000/0x100), $ this-> BackColor % 0x100); imagefilledrectangle ($ this-> _ image, 0, 0, $ this-> Width, $ this-> Height, $ this-> BackColor); imagecolordeallocate ($ this-> _ image, $ this-> BackColor ); if ($ transparent) {// Set transparent imagecolortransparent ($ this-> _ image, $ this-> BackColor) ;}}/*** rendered image or text, array, in the defined order. * The element is x, y, type = text | image, content = image path or text * @ param $ renders. * Mode: * array (* 'X' => 0, * 'y' => 0, * 'type' => 'text ', * 'content' => 'image path or text content', * // The additional attributes of the text below * 'font' => null, * 'fontsize' => 30, * 'angle '=> 0, * 'color' => 0x000000 *); */public function Render (array $ renders) {foreach ($ renders as $ r) {if (! Is_array ($ r) |! Isset ($ r ['type']) |! Isset ($ r ['content']) {continue;} $ type = $ r ['type']; $ x = isset ($ r ['x'])? $ R ['x']: 0; $ y = isset ($ r ['Y'])? $ R ['Y']: 0; $ ResizeRate = isset ($ r ['resizerate'])? $ R ['resizerate']: 1; $ AutoResize = isset ($ r ['autoresize'])? $ R ['autoresize']: 0; $ content = $ r ['content']; if ($ type = 'text ') {$ fontsize = isset ($ r ['fontsize'])? $ R ['fontsize']: 30; $ angle = isset ($ r ['ange'])? $ R ['angle ']: 0; $ foreColor = isset ($ r ['color'])? $ R ['color']: 0x000000; $ fontfile = isset ($ r ['font'])? $ R ['font']: null; $ this-> RenderText ($ content, $ x, $ y, $ fontsize, $ angle, $ foreColor, $ fontfile );} else {$ this-> RenderImage ($ content, $ x, $ y, $ ResizeRate, $ AutoResize) ;}}/ *** presents text. * @ Param string $ text. * @ Param integer $ x. * @ Param integer $ y ordinate. * @ Param integer $ size font size, in pixels. * @ Param integer $ angle. * @ Param integer $ foreColor text color. * @ Param string $ fontfile font file path. */Public function RenderText ($ text, $ x = 0, $ y = 0, $ size = 30, $ angle = 0, $ foreColor = 0x000000, $ fontfile = null) {$ c = func_num_args (); if ($ c = 1 & is_array ($ text) {extract ($ text );} if ($ text = '') {return;} $ fontfile = is_null ($ fontfile )? $ This-> FontFile: $ fontfile; $ foreColor = imagecolorallocate ($ this-> _ image, (int) ($ foreColor % 0x1000000/0x10000), (int) ($ foreColor % 0x10000/0x100), $ foreColor % 0x100); imagettftext ($ this-> _ image, $ size, $ angle, $ x, $ y, $ foreColor, $ fontfile, $ text); imagecolordeallocate ($ this-> _ image, $ foreColor);}/*** presents the image and outputs it in a simple manner. * @ Param string $ src image file. * @ Param integer $ x. * @ Param integer $ y ordinate. */Public function RenderImage ($ src, $ x = 0, $ y = 0, $ ResizeRate = 1, $ AutoResize = 1) {$ c = func_num_args (); if ($ c = 1 & is_array ($ src) {extract ($ src);} if ($ src = '') {return ;} $ meta = getimagesize ($ src); $ w = $ meta [0]; $ h = $ meta [1]; $ new_width = $ w/$ ResizeRate; $ new_height = $ h/$ ResizeRate; switch ($ meta [2]) {case IMAGETYPE_GIF: $ image = imagecreatefromgif ($ src); break; case IMAGETYPE_PNG: $ image = imagere Atefrompng ($ src); break; case IMAGETYPE_JPEG: default: $ image = imagecreatefromjpeg ($ src); break;} // imagecopy ($ this-> _ image, $ image, $ x, $ y, 0, 0, $ w, $ h); // simple output if ($ AutoResize = 1) {$ this-> Width; // Width of the current base map $ this-> Height; // Height of the current base map $ MaxSizeWidth = $ this-> Width-(2 * $ x ); // The maximum width of the scaled down Image $ MaxSizeHeight = $ this-> Height-(2 * $ y); // The maximum Height of the scaled down Image // echo $ MaxSizeWidth; // echo $ MaxSizeHeight; if ($ MaxSizeWidth >=$ w )& & ($ MaxSizeHeight >=$ h) // The Width and height are small and can be centered directly without narrowing down {$ outputSx = ($ this-> Width-$ w) /2; $ outputSy = ($ this-> Height-$ h)/2; imagecopyresized ($ this-> _ image, $ image, $ outputSx, $ outputSy, 0, 0, $ w, $ h, $ w, $ h);} else {$ DstWHRate = $ MaxSizeWidth/$ MaxSizeHeight; // the reduced aspect ratio $ SrcWhRate = $ w/$ h; // if ($ DstWHRate> = $ SrcWhRate) {$ ResizeRate = ($ h/$ MaxSizeHeight); // Get the zoom ratio} else {$ ResizeRate = ($ w/$ MaxSizeWidth); // Get the zoom ratio} $ New_width = ceil ($ w/$ ResizeRate); $ new_height = ceil ($ h/$ ResizeRate); $ outputSx = ceil ($ this-> Width-$ new_width) /2); $ outputSy = ceil ($ this-> Height-$ new_height)/2); imagecopyresized ($ this-> _ image, $ image, $ outputSx, $ outputSy, 0, 0, $ new_width, $ new_height, $ w, $ h) ;}// echo $ w; // echo $ h ;} else {imagecopyresampled ($ this-> _ image, $ image, $ x, $ y, 0, 0, $ new_width, $ new_height, $ w, $ h );} imagedestroy ($ I Mage);}/*** output content to the browser. */Public function Flush () {switch (strtoupper ($ this-> Type) {case 'jpg ': imagejpeg ($ this-> _ image); break; case 'GIF': imagegif ($ this-> _ image); break; default: imagepng ($ this-> _ image); break ;}} /*** output to the file or returned content. * @ Param string $ filename * @ Return void if the file name is not provided, the image content is returned. If a file name is provided, it is output to the file. */Public function Output ($ filename = null) {if (! Empty ($ filename) {switch (strtoupper ($ this-> Type) {case 'jpg ': imagejpeg ($ this-> _ image, $ filename); break; case 'GIF': imagegif ($ this-> _ image, $ filename); break; default: imagepng ($ this-> _ image, $ filename); break ;}} else {ob_start (); switch (strtoupper ($ this-> Type) {case 'jpg ': imagejpeg ($ this-> _ image); break; case 'GIF ': imagegif ($ this-> _ image); break; default: imagepng ($ this-> _ image); break;} $ R = ob_get_contents (); ob_end_clean (); return $ r ;}/ *** release the resource. it is also called implicitly when the object instance is detached. */Public function Dispose () {if (! Is_null ($ this-> _ image) {imagedestroy ($ this-> _ image); $ this-> _ image = null;} public function _ destruct () {$ this-> Dispose ();}/*** is displayed directly. * @ Param integer $ width: specifies the image width. * @ Param integer $ height the image height. * @ Param integer $ backColor background color. * @ Param array $ renders indicates the content, which is the same as the Render method definition. */Public static function DirectRender ($ width = 100, $ height = 50, $ backColor = 0 xFFFFFF, array $ renders, $ type = "jpg") {header ('pragma: public '); header ('expires: 0'); header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 '); header ('content-Transfer-Encoding: binary '); header ("Content-type: image /". ($ type = 'jpg '? 'Jpeg ': strtolower ($ type); $ B = new self ($ width, $ height, $ backColor); $ B-> Render ($ renders ); $ B-> Flush (); $ B-> Dispose ();}/*** present to a file or return image data. * @ Param integer $ width: specifies the image width. * @ Param integer $ height the image height. * @ Param integer $ backColor background color. * @ Param array $ renders indicates the content, which is the same as the Render method definition. * @ Param string $ the name of the file to be rendered by filename. If this parameter is not provided, the content is directly returned. */Public static function RenderTo ($ width = 100, $ height = 50, $ backColor = 0 xFFFFFF, array $ renders, $ filename = null) {$ B = new self ($ width, $ height, $ backColor); $ B-> Render ($ renders); $ r = $ B-> Output ($ filename ); $ B-> Dispose (); return $ r ;}// combines text and images and returns image data. ImageBuilder: DirectRender (900,700, 0xF0F0F0, array ('x' => 0, 'y' => 0, 'type' => 'image ', 'content' => 'bg/asw.gif '), array ('x' => 260, 'y' => 30, 'type' => 'image ', 'content' => '1.jpg '), array ('x' => 75, 'y' => 54, 'type' => 'text ', 'content' => 'waterline', 'fontsize '=> 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 75, 'y' => 86, 'type' => 'text ', 'content' => 'neutral ', 'fontsize' => 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 155, 'y' => 86, 'type' => 'text ', 'content' => 'otaku ', 'fontsize' => 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 75, 'y' => 116, 'type' => 'text ', 'content' => '000000', 'fontsize '=> 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 135, 'y' => 116, 'type' => 'text ', 'content' => '11', 'fontsize '=> 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 170, 'y' => 116, 'type' => 'text ', 'content' => '11', 'fontsize '=> 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 75, 'y' => 146, 'type' => 'text ', 'content' => 'shantun village, Xiantao city, Hubei province ', 'fontsize' => 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 75, 'y' => 166, 'type' => 'text ', 'content' => 'Number 110, Lane 8, Lane 6, egg 7 ', 'fontsize' => 12, 'font' => 'arialuni. ttf', 'color' => 0x000000), array ('x' => 145, 'y' => 219, 'type' => 'text ', 'content' => '000000', 'fontsize '=> 12, 'font' => 'arialuni. ttf', 'color' => 0x000000 )));

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.