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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:41:52+00:00 2026-05-26T01:41:52+00:00

I have written a script image.php that is quite long, but I’ve just copied

  • 0

I have written a script image.php that is quite long, but I’ve just copied a portion of the code below.

Note that:

$image = $_GET['ph'];
$w = $_GET['w'];

So that:

function getImage_w($image, $w){
    $ext = strrchr($image, ".");
    if (strtolower($ext) == '.jpg' || strtolower($ext) == '.jpeg'){
        header('Content-type: image/jpeg');

        $src_im_jpg = imagecreatefromjpeg($image);
        $size = getimagesize($image);

        $src_w = $size[0];
        $src_h = $size[1];
        $dst_w = $w;
        $dst_h = round(($dst_w/$src_w)*$src_h);
        $dst_im = imagecreatetruecolor($dst_w,$dst_h);

        imagecopyresampled($dst_im,$src_im_jpg,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);

        imagejpeg($dst_im);
    }
}

This script takes a two GET variables, $_GET['ph'] and $_GET['w'], ph defines the URL of the image, and w defines the destination resize width.

For example: If my script is in a libs/ directory, and my photos are in a resources directory, and I want to display my photo with width = 100, I have to use this html:

<img src="libs/image.php?ph=../resources/myimage.jpg&w=100">

My problem is, my images appear in the browser correctly, but with a bad url format. I’d like to put images alt attribute instead of this url in the images property.

And to automatically apply the image.php for an image request.

Also, I have a problem when the user wants to save the image in his desktop, the image saves under a name image.php, which is not desirable.

Thank you in advance.

  • 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-26T01:41:53+00:00Added an answer on May 26, 2026 at 1:41 am
    RewriteEngine on
    #change 'resize' to the folder your image.php is in
    RewriteBase /resize
    
    #e.g. /resize/path/to/image/sample-200.jpg
    # will become /resize/image.php?ph=path/to/image/sample.jpg&w=200
    RewriteRule ^(.*)-([0-9]+)(\.jpg)$ image.php?ph=$1$3&w=$2 [L,NC]
    

    Note that you can’t use .. in the url, so instead use root-relative url. To do this you need to add the document_root the the path in you php file:

    $image = $_SERVER['DOCUMENT_ROOT'] . "/" . str_replace('..','', $_GET['ph']);
    

    Users saving the image will get the normal image name, but with a ‘-‘ and the size added to the end.

    Not sure what you want with the alt. Also I don’t think it is wise to rewrite all images, so this script only rewrites images the start with /resize/…

    http://www.example.com/hosting.jpg is not possible, as the php doesn’t know in which folder to look for this image, and to what size to resize it.

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

Sidebar

Related Questions

I have written a CGI script that creates an image dynamically using GET data.
I have a written a script in PHP that reads from data from a
I have written a jQuery image resize script that works fine when the images
I have written a script that goes through a bunch of files and snips
I have written a watir script that downloads files. One of the files it
I have written a Perl script, I just want to give it to every
I've written a PHP script that runs via SSH and nohup, meant to process
I have written a pretty basic upload script that takes the file and uploads
I have a script that I've written that I sell to others who want
I have written a script using my local PHP 5.3 installation making use of

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.