Saving created image

Posted 3 months ago by cservices

I have an image in my public storage storage/app/public/alkami/backgrounds

I am adding text to the image and creating the image with imagepng($png_image)

Question is, how do I save this image to disc at storage/app/public/alkami

I'm using Laravel 5.4, as my PHP version is 5.something

Here is the full method I am using to create the text over image

public function generateImage()
    {
        //Set the Content Type
        header('Content-type: image/png');

        // Create Image From Existing File
        $png_image = imagecreatefrompng(asset(request()->image_name));

        if (request()->font_color == "#fff") {
            // Allocate A Color For The Text
            $font_color = imagecolorallocate($png_image, 255, 255, 255);
        }

        if (request()->font_color == "#000") {
            // Allocate A Color For The Text
            $font_color = imagecolorallocate($png_image, 0, 0, 0);
        }

        // Set Path to Font File
        $font_path = '/fonts/Arial Bold.ttf';
        $font_path2 = '/fonts/Arial Italic.ttf';

        if (isset(request()->ad_text_one)) {
            // Set Text to Be Printed On Image
            $text = request()->ad_text_one;

            // Print Text On Image
            imagettftext($png_image, request()->ad_text_one_size, 0, request()->ad_text_one_left, request()->ad_text_one_top, $font_color, $font_path, $text);
        }

        if (isset(request()->ad_text_two)) {
            // Set Text to Be Printed On Image
            $text = request()->ad_text_two;

            // Print Text On Image
            imagettftext($png_image, request()->ad_text_two_size, 0, request()->ad_text_two_left,  request()->ad_text_two_top, $font_color, $font_path, $text);
        }

        if (isset(request()->ad_text_three)) {
            // Set Text to Be Printed On Image
            $text = request()->ad_text_three;

            // Print Text On Image
            imagettftext($png_image, request()->ad_text_three_size, 0, request()->ad_text_three_left,  request()->ad_text_three_top, $font_color, $font_path2, $text);
        }

        // Send Image to Browser
        $image = imagepng($png_image);

        // Clear Memory
        imagedestroy($png_image);
    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.