Send portion of an image to client in PHP -


hi have huge image on php server, represents map in web game. need client request small portion of image. client request cant load whole image because: 1) suffocate server
2) dont want client know how whole map looks like.

would imagecopy function satisfy needs? in case dont know how use it... have snippet:

<?php             // create image instances     $src = imagecreatefromgif('images\map.gif');     $dest = imagecreatetruecolor($mapvisiblewidth, $mapvisibleheight);       // copy     imagecopy($dest, $src, 0, 0, x, y, mapvisiblewidth, mapvisiblewidth);                      // output , free memory     header('content-type: image/gif');     imagegif($dest);      imagedestroy($dest);     imagedestroy($src);      ?>  

thanks!

i don't have large map image or have..

but example searched on google , got random image meets need.

step 1 :

use imagecreatefrompng generate image .png file

$srcp = imagecreatefrompng("http://www.clker.com/cliparts/o/f/g/g/s/t/curly-frame-hi.png"); 

you may point url has png image

step 2 :

set size of image created

$destp = imagecreate(150, 150); 

step 3 :

make copy custom settings

imagecopyresampled ($destp, $srcp, 0, 0, 8, 8, 150, 150, 8, 8);

step 4 :

set header type , generate

header('content-type: image/png'); imagepng($destp); 

so code

<?php     $srcp = imagecreatefrompng("http://www.clker.com/cliparts/o/f/g/g/s/t/curly-frame-hi.png");     $destp = imagecreate(150, 150);     imagecopyresampled ($destp, $srcp, 0, 0, 8, 8, 150, 150, 8, 8);     header('content-type: image/png');     imagepng($destp); ?> 

note :

  1. you can follow same little modification other formats, size , image.

  2. by way user can't image path or right click open full image , yes secure think.

update :

as op needs display heading, title etc.,

as functions create headers , html elements default don't need worry it.

but if want it, can use iframe, php page can loaded in page wanted.

here's short example of it.

keep below file source.php

<?php     $srcp = imagecreatefrompng("http://www.clker.com/cliparts/o/f/g/g/s/t/curly-frame-hi.png");     $destp = imagecreate(150, 150);     imagecopyresampled ($destp, $srcp, 0, 0, 8, 8, 150, 150, 8, 8);     header('content-type: image/png');     imagepng($destp); ?> 

and have page as display.html

you can make iframe this

<!doctype html> <html> <head>     <title>image display viewer</title> </head>  <style type="text/css" media="screen">         body,         html {             width: 100%;             height: 100%;             overflow: hidden;         }          * {             padding: 0;             margin: 0;         }          iframe {             width: 960px;             height: 100%;             overflow: hidden;             border: none;         }     </style> <body> <iframe src="source.php" scrolling="0"></iframe> </body> </html> 

note : though iframe not idea, still user can't able see other part of image. means still you're secure :)


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -