Php Getting Started Tutorial 7 (image operations and examples)

Source: Internet
Author: User
Tags imagejpeg php tutorial

This article uses a large number of examples to explain the php Tutorial code for image operations. Next let's take a look at the example tutorials.

<? Php
$ Height = 300;
$ Width = 300;
// Create a background image
$ Im = imagecreatetruecolor ($ width, $ height );
// Assign color
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Blue = imagecolorallocate ($ im, 0, 0, 64 );
// Draw the color to the image
Imagefill ($ im, 0, 0, $ blue );
// Draw the string: hello, php
Imagestring ($ im, 10,100,120, 'Hello, php', $ white );
// Output the image and define the header
Header ('content-type: image/png ');
// Send the image to the browser
Imagepng ($ im );
// Clear resources
Imagedestroy ($ im );
?>

Create a thumbnail for instance 2

<? Php
Header ("content-type: image/jpeg ");
// Load the image
$ Imagen1 = imagecreatefromjpeg ("imagen1.jpg ");
$ Imagen2 = imagecreatefromjpeg ("imagen2.jpg ");

// Copy the image
Imagecopy ($ imagen1, $ imagen2, 200,150, 0 );

// Output jpeg image
Imagejpeg ($ imagen1 );

// Release the memory
Imagedestroy ($ imagen2 );
Imagedestroy ($ imagen1 );

?>

Obtain image size information

<? Php
$ Info = getimagesize ("imagen2.jpg ");
Print_r ($ info );

?>

Create a png image

<? Php
// Png format image processing function
Function loadpng ($ imgname ){
$ Im = @ imagecreatefrompng ($ imgname );
If (! $ Im) {// image loading failed
$ Im = imagecreate (400, 30 );
$ Bgc = imagecolorallocate ($ im, 255,255,255 );
$ Tc = imagecolorallocate ($ im, 0, 0, 0 );
Imagefilledrectangle ($ im, 0, 0,150, 30, $ bgc );
Imagestring ($ im, 4, 5, 5, "error loading: $ imgname", $ tc );
    }
Return $ im;
 }
$ Imgpng = loadpng ("./karte.png ");
/* Output the image to the browser */
Header ("content-type: image/png ");
Imagepng ($ imgpng );
?>


Add text to images

<? Php
// Create a 100*30 image
$ Im = imagecreate (100, 30 );
// White background and blue text
$ Bg = imagecolorallocate ($ im, 200,200,200 );
$ Textcolor = imagecolorallocate ($ im, 0, 0,255 );
 
// Write the string at the top left
Imagestring ($ im, 5, 0, 0, "hello world! ", $ Textcolor );
 
// Output the image
Header ("content-type: image/jpeg ");
Imagejpeg ($ im );
Imagedestroy ($ im );

?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.