Creating an image

suggest change

To create a blank image, use the imagecreatetruecolor function:

$img = imagecreatetruecolor($width, $height);

$img is now a resource variable for an image resource with $widthx$height pixels. Note that width counts from left to right, and height counts from top to bottom.

Image resources can also be created from image creation functions, such as:

Image resources may be freed later when there are no more references to them. However, to free the memory immediately (this may be important if you are processing many large images), using imagedestroy() on an image when it is no longer used might be a good practice.

imagedestroy($image);

Converting an image

Images created by image conversion does not modify the image until you output it. Therefore, an image converter can be as simple as three lines of code:

function convertJpegToPng(string $filename, string $outputFile) {
    $im = imagecreatefromjpeg($filename);
    imagepng($im, $outputFile);
    imagedestroy($im);
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Image processing with GD:
* Creating an image

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
73 Image processing with GD
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR