PHP provides multiple functions to create images from scratch and they can be used to generate images of different extensions (JPEG, GIF, PNG, etc...).

The imagecreatefromjpeg() function

An image can be created from a file or URL by a function imagecreatefromjpeg(). which will create an image of an type (extension) JPEG.

Example of creating a JPG (JPEG) image with PHP

<?php
    function createJPEG($imagename)
    {
        $image = @imagecreatefromjpeg($imagename);
        if(!$image)
        {
            $image = imagecreatetruecolor(150, 30);
            $bgc = imagecolorallocate($image, 255, 255, 255);
            $tc = imagecolorallocate($image, 0, 0, 0);
            
            imagefilledrectangle($image, 0, 0, 150, 30, $bgc);
            
            imagestring($image, 1, 5, 5, "Error loading image" . $imagename, $tc);
        }
        return $image;
    }
    
    header("Content-Type: image/jpeg");
    
    $image = createJPEG("fake_image.png");
    
    imagejpeg($image);
    imagedestroy($image);
?>

 

›› go to examples ››