Logo
Get Image From URL and Display it
Online Now: 0
Snippet Code
This snippet uses cURL to get a graphic and display a thumbnail image on the page without saving it.

displayImg.php


<?php
//$url = "http://share.meebo.com/content/katy_perry/wallpapers/3.jpg";
$url $_GET['url'];

$url str_replace("http:/","http://",$url);

$allowed = array('jpg','gif','png');
$pos strrpos($_GET['url'], ".");
$str substr($_GET['url'],($pos 1));

$ch curl_init();
$timeout 0;
curl_setopt ($chCURLOPT_URL$url);
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);

// Getting binary data
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_BINARYTRANSFER1);

$image curl_exec($ch);
curl_close($ch);
// output to browser
$im = @imagecreatefromstring($image);

$tw = @imagesx($im);
if(!
$tw){
    
// Font directory + font name
    
$font '../../fonts/Austrise.ttf';
    
// Size of the font
    
$fontSize 18;
    
// Height of the image
    
$height 32;
    
// Width of the image
    
$width 250;
    
// Text
    
$str 'Couldn\'t get image.';
    
$img_handle imagecreate ($width$height) or die ("Cannot Create image");
    
// Set the Background Color RGB
    
$backColor imagecolorallocate($img_handle255255255);
    
// Set the Text Color RGB
    
$txtColor imagecolorallocate($img_handle2092137); 
    
$textbox imagettfbbox($fontSize0$font$str) or die('Error in imagettfbbox function');
    
$x = ($width $textbox[4])/2;
    
$y = ($height $textbox[5])/2;
    
imagettftext($img_handle$fontSize0$x$y$txtColor$font $str) or die('Error in imagettftext function');
    
header('Content-Type: image/jpeg');
    
imagejpeg($img_handle,NULL,100);
    
imagedestroy($img_handle); 
}else{
    if(
$str == 'jpg' || $str == 'jpeg')
        
header("Content-type: image/jpeg");
    if(
$str == 'gif')
        
header("Content-type: image/gif");
    if(
$str == 'png')
        
header("Content-type: image/png");
    
$th imagesy($im);
    
$thumbWidth 200;
    if(
$tw <= $thumbWidth){
        
$thumbWidth $tw;
    }
    
$thumbHeight $th * ($thumbWidth $tw);
    
$thumbImg imagecreatetruecolor($thumbWidth$thumbHeight);
    if(
$str == 'gif'){
        
$colorTransparent imagecolortransparent($im);
        
imagefill($thumbImg00$colorTransparent);
        
imagecolortransparent($thumbImg$colorTransparent);
    }
    if(
$str == 'png'){
        
imagealphablending($thumbImgfalse);
        
imagesavealpha($thumbImg,true);
        
$transparent imagecolorallocatealpha($thumbImg255255255127);
        
imagefilledrectangle($thumbImg00$thumbWidth$thumbHeight$transparent);
    }
    
imagecopyresampled($thumbImg$im0000$thumbWidth$thumbHeight$tw$th);


    if(
$str == 'jpg' || $str == 'jpeg'){
        
imagejpeg($thumbImgNULL100);
    }
    if(
$str == 'gif'){
        
imagegif($thumbImg);
    }
    if(
$str == 'png'){
        
imagealphablending($thumbImg,TRUE);
        
imagepng($thumbImgNULL9PNG_ALL_FILTERS);
    }
        
    
imagedestroy($thumbImg);
}
?>

Get Image Directly


displayImg.php?url=http://phpsnips.com/images/phpsnippets.jpg

Get Image Using A Tag


<img src="displayImg.php?url=http://phpsnips.com/images/phpsnippets.jpg" />
 
Snippet Comments

Add Your Comment

hebergement web
2011-03-30 04:36:44
Great code man ! Thanks a lot
pet supplies review
2011-02-26 23:34:32
Great addition
women car insurance
2011-02-24 23:01:07
I like this snippet, small tiny helpful code.
hazemeil
2011-02-24 11:09:29
let me use this. i want to try if this works. hazemeil
Rowing Machine Reviews
2011-02-04 08:19:17
Can you add in some sort of regex to allow for grabbing of related images from the same site or google search results?
UTAN
2011-02-04 08:09:22
I liked the code.. I also right tutorial at my blog... its in spanish language though thanks so much.
articletech
2011-02-02 05:15:07
that is the correct code, good job.
Washington Redskins jerseys
2011-01-27 23:29:01
And you have got a design here that is not too flashy, but it's makes a statement as big as what you are saying.
online casino reviews
2011-01-17 00:30:44
I'll use this code in my project for Web-systems and technologies, thanks a lot!
NZ Free Web Hosting
2011-01-16 20:30:43
Just the code I was after thanks heaps.
One IT - NZ Cloud Hostin
2011-01-16 20:29:40
Great code my man. Was looking everywhere for this.
Anonymous
2011-01-06 15:13:35
Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\1.php on line 11
Divorce Lawyer New York
2011-01-05 08:18:43
it's very useful code.
silver and gold
2011-01-03 02:48:56
You really know your stuff... Keep up the good work!
Games Online
2010-12-31 04:17:12
This code isn't working for me!
Recepti
2010-12-31 00:49:34
Thanks for giving us the code on the display image.
professional seo
2010-12-14 20:35:47
I found that the code works if you //remove the str_replace on line 4.
website optimizer
2010-12-14 20:35:14
I will try to create a fancier one though for future reference.
vir0e5
2010-11-24 21:25:23
this code dosn't work...sir!!!
php code
2010-09-14 00:33:04
it`s useful,bu i don\'t understand this code.

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Beginner
Current Score: 2.8
Total votes: 90
Total Views: 32596

Other top snippets by admin:

1. JPEG to ASCII Converter
2. Add (th, st, nd, rd, th) to the end of a number
3. Dynamic Page Content From Links
4. AJAX Quickie
5. Simple Image CAPTCHA

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative