Creating an image
suggest changeTo create a blank image, use the imagecreatetruecolor function:
$img = imagecreatetruecolor($width, $height);
$img is a GD image with $width by $height pixels. Through PHP 7 that value is a resource; PHP 8.0 and later return a GdImage object. 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:
imagecreatefrompngimagecreatefromjpeg- other
imagecreatefrom*functions.
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($img);
Converting an image
Converting an image does not change the source file. Decode it, write the new format, and free the image:
function convertJpegToPng(string $filename, string $outputFile) {
$im = imagecreatefromjpeg($filename);
imagepng($im, $outputFile);
imagedestroy($im);
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents