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?

Ilmari Karonen
  • 44,762
  • 9
  • 83
  • 142
cubryto DTH
  • 53
  • 1
  • 6

2 Answers2

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 );
Ilmari Karonen
  • 44,762
  • 9
  • 83
  • 142
  • @Susan: Why would you edit that? `echo` can [output multiple strings separated by commas](http://stackoverflow.com/questions/1466408/difference-between-and-in-php) just fine, and [it's even slightly more efficient](http://www.fusionswift.com/2010/05/php-concatenation-benchmark-comma-vs-period/) than concatenating the parameters into a single string first. (Thanks for the upvote, though!) – Ilmari Karonen Mar 27 '14 at 01:22
  • 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. – sbuck Apr 05 '14 at 20:25
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.

Diodeus - James MacFarlane
  • 107,156
  • 31
  • 147
  • 171
  • 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. – cubryto DTH Jan 24 '14 at 21:03
  • 1
    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. – Diodeus - James MacFarlane Jan 24 '14 at 21:06
  • 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. – cubryto DTH Jan 24 '14 at 21:12
  • 1
    A data uri is good for that. Or you can proxy the image through a program which will send out a MIME header for the image, then stream the data, thus masking the source. – Diodeus - James MacFarlane Jan 24 '14 at 21:25