Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 583007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:44:50+00:00 2026-05-13T14:44:50+00:00

I’m looking at this code and have been through the docs but still don’t

  • 0

I’m looking at this code and have been through the docs but still don’t understand how this should work. The code works fine just as it sits but I’m wondering if I should be outputting the header as png instead of jpeg.

What exactly is going on in this code? Is the png image converted to jpeg?

What I ultimately want to do is to watermark all gif, jpg, bmp and png images in a single directory. I’m outputting all headers, regardless of the image type as jpg. Is this correct? I hope I’m making sense here, I’m kind of tired.

$im2 = imagecreatefrompng($image)
imagecopy() and more code here
header("Content-Type: image/jpeg");
imagejpeg($im2,'',50);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:44:50+00:00Added an answer on May 13, 2026 at 2:44 pm

    What your code does is more or less as follows:

    // Load the PNG file from disk into memory
    $im2 = imagecreatefrompng($image) 
    

    $im2 is now a resource, referencing a image. Once in memory, it is not a png or a jpeg; it is a raw, uncompressed data. The “format” of an image specifies how that raw data is packaged and formatted; at this point, it has no such formatting. It’s just data in memory.

    // Some code which works with the image in memory, adding your watermark?
    imagecopy() and more code here
    
    // Tell the browser that we're output a JPG
    header("Content-Type: image/jpeg");
    

    If you request a jpg (ie http://host.com/image.jpg) then the server takes care of writing this header for you. If you’re making a JPG on the fly via PHP you have to manually output the header. Otherwise, PHP assumes you’re writing HTML and outputs the appropriate headers for you as soon as write anything to stdout, either via echo or just by having text/whitespace outside of <?php ?> tags.

    // compress as a jpeg, and send to browser
    imagejpeg($im2,'',50);
    

    imagejpeg takes the raw image, compresses it as a jpg, and writes it to either a file (if you give it a filename) or stdout (which sends it to the browser). Technically to output to the browser, the 2nd argument should be null, not ''. The final parameter, 50, specifies the jpeg quality as percentage. 100 is high-quality, 0 is low quality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 398k
  • Answers 398k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check up on the Law of Demeter. May 15, 2026 at 3:35 am
  • Editorial Team
    Editorial Team added an answer Classic mode models the IIS 6.0 model in which ASP.NET… May 15, 2026 at 3:35 am
  • Editorial Team
    Editorial Team added an answer Working Example: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">… May 15, 2026 at 3:35 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.