0

I came across a situation where previous developer used php file in Image source.Here is code for that

<img class="large_image" src="http://www.example.com/common/resize.php?img=4_jisokupo_jpg&amp;d=467&amp;w=461&amp;h=307" id="large_image_1" style="display: inline;">

When I Copy source of image tag and run in browser and check its source code,It again give me Image tag again

<img class="decoded" src="http://www.example.com/common/resize.php?img=4_jisokupo_jpg&amp;d=467&amp;w=461&amp;h=307" alt="">

Please guide what they are doing?Is it img tag inside img src?

I know this is common question But i am new to PHP.

UPDATED:Can we use php file in src? what it suppose to do in php file?

3
  • When I Copy source of image tag and run in browser and check its source code,It again give me Image tag again What does that mean? Can you describe what it is exactly that you're doing, and what you're expecting? The browser doesn't know or care that PHP is on the server. PHP generates an image on the fly (derived from another image file in this case), and returns it to the browser. Commented Jun 8, 2014 at 7:13
  • I don't think you understand what's happening OP. Commented Jun 8, 2014 at 7:18
  • Simply i ment that they have used path to php file passing width and height of image they want.when i run that php file path in browser it give me img tah Commented Jun 8, 2014 at 7:37

3 Answers 3

1

Actually you can use PHP to generate an image, for example:

$contents = file_get_contents('path_to_an_image.jpeg');
header('Content-type: image/jpeg');
echo $contents;

This is just an example to clarify you but in your question the case is different. It looks like that, http://www.example.com/common/resize.php is being called and the query string is being passed to that script so that script can access those parameters using:

// in resize.php
$img = $_GET['img']; // 4_jisokupo_jpg
$d = $_GET['d']; // 467
$w = $_GET['w']; // 461
$w = $_GET['h']; // 307

Then after resizing the image the new image would be returned, check imagejpeg / imagecreatefromjpeg for more ideas. So, when the script is running to echo the <img src='some.php' />, the some.php file will be executed to deliver the image and the dynamic image will be printed in the browser.

Check the following video on YouTube (PHP Custom Image Cropping Tutorial) and get more idea about image manipulation using PHP.

Sign up to request clarification or add additional context in comments.

2 Comments

What if the image is not jpeg? Will this code will be ok?header('Content-type: image/jpeg');
No, there are different functions for different images. Check GD and Image Functions.
0

When you view source in your web-browser you're viewing the interpreted php/html/etc. output of the PHP that generated it; this is the reason you are confused. I would recommend finding some introductory lessons on PHP to gain a better understanding of how things work from both a server and client perspective.

              SERVER
      .----Website (PHP)-----------------------------------------------.
      |        |                                                       |
      |        |                                                       |
      |        |______Image Class "large_image"                        |
      |                          |                                     |
      |                          |                                     |
      |                          |____source "some large image"        |
      |                                          |                     |
       \_______  ________________________________|_____________________/
               ⬇

             CLIENT    
      .------Web Browser (rendered PHP)--------------------------------.
      |        |                                                       |
      |        |                                                       |
      |        |_______Image Class "decoded"                           |
      |                          |                                     |
      |                          |                                     |
      |                          |_____source "same large image"       |
      |                                                                |
      \________________________________________________________________/

9 Comments

If you check src tag,it has PHP file path with some parameters..what they are doing there in php?
The class="large_image" is created most likely as a style for the css to act upon. On the end of the source url ,resize.php?img=4_jisokupo_jpg&amp;d=467&amp;w=461&amp;h=307 resizes the image to 461 x 307, the id is a tag as you had observed, and the display:inline is a css directive to align the image within the div or span that it's in.
resizes the image to 461x307,Can we do this to show image directly into brower?As in resize.php?
Yes, try changing the numbers on the end, like http://www.example.com/common/resize.php?img=4_jisokupo_jpg&amp;d=667&amp;w=861&amp;h=507
what will be inside resize.php?It will be simple img tag?
|
0

What is going on when you copy the src from the html and put it in the browser, is that you are opening the image that is created on the fly by PHP in your browser.

As you are opening the image directly in the browser, the browser generates some very simple and basic html in its DOM, to show you that images that you have opened directly. That HTML is what you see when you Inspect Element, and go to the HTML tab. If you pay close attention you will not even see a "view source" button, once opening the image directly. You only see the Inspect Element button.

That HTML is NOT the source code, but just the DOM representation of the 'page' you are looking at.

In regards to your EDIT:

So, the php file in the src is a file that creates an image on the fly, and outputs its binary data. This is possible and explained a bit more in the answer of WereWolf - The Alpha. Your browser recognizes that binary data, and renders an image out of it. (Partly because it knows it was opened as an 'src' of an 'img' tag, so it is expecting that url will return an image. But mainly because of the 'content-type' header that was sent along is also telling that the data is an image.)

It just when the browser shows you this binary data as an image, the browser itself generates some HTML in the DOM so the image is displayed as an 'html' page. Because, hey, that's was browser do, they want to show you HTML :)

1 Comment

How and why they have php file in img src? what they supposed to do in php file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.