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 4035946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:09:44+00:00 2026-05-20T12:09:44+00:00

I’m trying to dynamically rename a jpeg image file as it’s displayed to the

  • 0

I’m trying to dynamically rename a jpeg image file as it’s displayed to the user.

I have a file image_generate.php with the following code:

$file = $_GET['file'];
$imagepath = "path/to/image.jpg";
$image = imagecreatefromjpeg($imagepath);
header('Content-Type: image/jpeg');

$filename = "[site.com]_some_image_name_here";
header('Content-Disposition: attachment; filename="' . $filename . '.jpg"');

imagejpeg($image, NULL, 100);
imagedestroy($image); // Free up memory

And it’s called by an html image tag like so:

<img src="image_generate.php?file=imagenamehere" />

So far, the output has the following results:

  • When I right-click on the image, and click “View Image,” a download dialogue pops up and asks if I’d like to save the file “[site.com]_some_image_name_here.jpg” (I want this to happen)
  • If I right-click on the image, and click “Save Image As,” or “Save Image,” the filename that’s to be saved shows up as the original filename (whatever the variable $file was able to fetch).

How can I fix the second part? I’d like to modify the filename of the image even when the user clicks “Save Image As” or “Save Image.”

I DID try to change the code to this:

...

$rename = $filename . ".jpg";
imagejpeg($image, $rename, 100);
imagedestroy($image); // Free up memory

But the image fails to show up on the page with the html tag (shows up as a broken image).
I’m not very familiar with html headers and Content-Dispositions. I’m guessing there’s an error in there somewhere..?
Any ideas?

Thank you for reading!

Edit: .htaccess below

<IfModule mod_rewrite.c>

RewriteEngine On

# Sends image download request to image_generate.php for parsing
RewriteRule ^path/from/img/tag/downloads/(.*).jpg$ path/to/image_generate.php?file=$1 [NC,L]
RewriteRule ^path/to/image_generate.php$ /home/path/to/image_generate.php [NC,L]

</IfModule>
  • 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-20T12:09:45+00:00Added an answer on May 20, 2026 at 12:09 pm

    Different browsers will have different behaviors.

    For example, Internet Explorer doesn’t try to fetch any header information before displaying the save file dialog when doing a Right Click > Save Image As (It automatically assumes you want to save the result of the request, regardless of what the result is). Neither does Firefox. Therefore, for IE & Firefox, it will be impossible to specify the filename by specifying it in the headers.

    A more elegant and UA-compatible way of doing this would be by using Apache’s mod_rewrite to do URL rewriting. URL rewriting allows you to reroute a request made to a URL towards another.

    To set this up, you need to create a .htaccess file in the directory where image_generate.php is located containing the following:

    RewriteEngine On
    RewriteRule ^generated/([a-z0-9-]+)\.jpg$  generated_image.php?file=$1  [L]
    

    Then, simply modify your HTML to point to your rewritten URL.

    <img src="generated/imagenamehere.jpg" />
    

    This also have the advantage of making your image generation completely seamless to the user (URL wise, it looks simply like a static image).

    More information about mod_rewrite can be found here:

    • mod_rewrite – Apache HTTP Server
    • mod_rewrite Cheat Sheet (V2) – Added Bytes by Dave Child
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put

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.