2

I wonder if it is possible to display a part of image using file_get_contents.

Currently I'm using this code to display the image without revealing its link/location

<img src="data:image/gif;base64,<?php echo base64_encode(file_get_contents($image)); ?> ">

Is there a way to just display 1/3 or 1/2 of the image using file_get_contents?

2 Answers 2

2

To do this, you need to decode, crop and re-encode the image. In PHP, you can use the GD library to do this, e.g.:

# load source image and get its size
$img = imagecreatefromgif( $image_file_name );
$width = imagesx( $img );
$height = imagesy( $img );

# create a copy showing only the top half of the image
$cropped = imagecreate( $width, $height / 2 );
imagecopy( $cropped, $img, 0,0, 0,0, $width, $height/2 );

# output image in GIF format emdebbed on the page
# XXX: GD doesn't seem to support output to string directly, but we can hack it
ob_start(); imagegif( $cropped ); $gif = ob_get_clean();
echo '<img src="data:image/gif;base64,', base64_encode( $gif ), '">';

# free the image objects once they're no longer needed
imagedestroy( $img );
imagedestroy( $cropped );
Sign up to request clarification or add additional context in comments.

2 Comments

@Susan: Why would you edit that? echo can output multiple strings separated by commas just fine, and it's even slightly more efficient than concatenating the parameters into a single string first. (Thanks for the upvote, though!)
My bad..If I remember correctly I thought in my tests that it was erroring out, but just tested again and (as expected) it works a-ok. Must have been barking up the wrong tree at the time.
1

The browser will not decode partial images. You can use various CSS techniques to hide the remaining areas of the image, such as wrapping your image in a DIV, setting CSS width + height with overflow:hidden, or setting it as a CSS background and setting the dimensions of that element.

Alternately, you can render it in canvas.

4 Comments

I want to hide the image's content so css technique will not work. I guess I can either use some sort of php library to crop the image in binary, or make an another cropped image file.
Anyone can sniff the data uri on the client and grab the image, regardless of what client-side technique you use. The only way to truly hide things is not to deliver them to the client.
Sorry, I didn't make it clear enough. The image is to display to registered member. What I'm trying to archive is to display a part of the image to non-registered member. I don't mind if they grab the image, I just don't want them to be able to track the location of the image's folder.
A data uri is good for that. Or you can proxy the image through a program <img src="sonething.php?image=12345"> which will send out a MIME header for the image, then stream the data, thus masking the source.

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.