Go Back   Cyber Tech Help Support Forums > Software > Web Development & Graphic Design

Notices

Reply
 
Topic Tools
  #1  
Old August 20th, 2006, 01:04 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
php image

HI

I would help so that I can put link to one image in one php file and than set source, like <img src="image.php"> and image will show up. I was trying with simple echo but it doesn't work when I source it...
Reply With Quote
  #2  
Old August 20th, 2006, 02:39 PM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
What code are you using to create the image?

You need to use PHP to create or output an image either by the inbuilt image functions or the header content type.
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #3  
Old August 20th, 2006, 02:59 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
what I want to do is that somebody hosts image on some free hosting sites and then he can create short link for image, like http://www.cybertechhelp.com/images/...ation/boat.gif and he/she enters into form and get out short link for image, something like image.php?boat. And now for avatar we can set link to image or in html <img src="image.php?boat"> and we get boat.gif image
Reply With Quote
  #4  
Old August 20th, 2006, 08:28 PM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
There are different ways to do it.

Here is a basic example where you take the querystring variable name and add the file extension and echo the image

http://domain.com/index.php?imagename
PHP Code:
<?php
if($_GET != ""){
 foreach(
$_GET as $key => $value){
  echo 
$key '<br>';
 }
 
 
$filename 'images/' $key '.gif';
 if (
file_exists($filename)) {
    echo 
'<img src="' $filename '" alt="' $filename '" />';
 } else {
    echo 
"The file $filename does not exist";
 }
}
?>

and here is an example to output a image using the header function

e.g.
Code:
<img src="http://domain.com/index.php?imagename" alt="myimage">
PHP Code:
<?php
Header
("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");
Header("Content-Type: image/gif");
foreach(
$_GET as $key => $value){
$f 'images/' $key '.gif';
}
 
if(!
readfile($f)) // Read the image
readfile("images/error.gif"); // If the script can't find the directory, display this image
?>
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #5  
Old August 20th, 2006, 10:25 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
the second option will work but is there anyway to also support other image formats? jpeg and png?
Reply With Quote
  #6  
Old August 20th, 2006, 10:32 PM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
You could reference the exension in the url rather than hardcode it.

The other way would be to use string functions to find a match for the filename and then use the mime_content_type function to get the file type. You could do this using a select case
http://uk2.php.net/manual/en/functio...ntent-type.php
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #7  
Old August 20th, 2006, 10:49 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
hm well if I'm running this on my home server it works with images located on pc. If I put it on server it says that can't show image because of mistakes.

http://url.si/test/image.php?http://...tons_cth/reply
Reply With Quote
  #8  
Old August 20th, 2006, 10:53 PM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
You can't use readfile to open remote files. It for local files only. You may have luck with one of the other file opening fuctions such as fopen.
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #9  
Old August 20th, 2006, 10:58 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
is it possible for little more help. With fopen I can't open even local files
Reply With Quote
  #10  
Old August 20th, 2006, 11:37 PM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
Here is an example for loading remote jpeg images
http://phparadise.de/php-code/image-...offsite-image/
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #11  
Old August 21st, 2006, 08:43 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
can't make it to work...
Reply With Quote
  #12  
Old August 22nd, 2006, 10:07 AM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
Maybe your PHP install does not have the required modules to do it.
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #13  
Old August 22nd, 2006, 10:58 AM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
Here is another example

Code:
http://domain.com/index.php?http://images.cybertechhelp.com/i/ban2.gif
PHP Code:
<?
// Set the URL you want to connect to
//$url = "http://www.google.co.uk/intl/en_uk/images/logo.gif";
/*
if(isset($_GET['image'])){
 $url = $_GET['image'];
}
*/
if(!$_SERVER['QUERY_STRING'] == ""){
 
$url $_SERVER['QUERY_STRING'];
}
// Check to see if the file exists by trying to open it for read only
if (!@fopen($url"r")) {
 
$url "error.gif";
}
 
$ext strtolower(substr($url,-4));
 
 switch(
$ext){
  case 
".gif";
   
$type "image/gif";
   break;
  case 
".jpg";
   
$type "image/jpeg";
   break;
  case 
".png";
   
$type "image/png";
   break;
  }
 
 
header("Content-Type: $type"); 
$handle readfile($url);
?>
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
  #14  
Old August 22nd, 2006, 05:01 PM
oracle128's Avatar
oracle128 oracle128 is offline
Α Ω
 
Join Date: Oct 2000
O/S: Windows XP Pro
Location: Melbourne, Australia
Age: 24
Posts: 9,401
Quote:
Originally Posted by degsy
PHP Code:
$ext strtolower(substr($url,-4)); 
Might want to tokenize that instead degsy, to handle extensions that aren't 3 characters long (eg. ".jpeg"):
PHP Code:
$tokens explode(".",$url);
$ext strtolower(count($tokens)-1); 
Will need to change the switch/case and drop the ".", or keep it and just insert one before $ext.
__________________
Oracle's backup tutorial

"A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos.

"A problem well stated is a problem half solved" - Charles Franklin Kettering

Last edited by oracle128; August 22nd, 2006 at 05:04 PM.
Reply With Quote
  #15  
Old August 22nd, 2006, 06:51 PM
degsy's Avatar
degsy degsy is offline
Cyber Tech Help Moderator
 
Join Date: Jul 2001
Location: North-East, UK
Posts: 22,022
Blog Entries: 1
for this simple script it doesn't really need it.

e.g.
PHP Code:
case ".jpg";
  case 
"jpeg";
   
$type "image/jpeg";
   break; 
if you did want to do it by the finding the extensions on it's own then you can bypass the explode
PHP Code:
strtolower(substr(strrchr($url,"."),1)); 
__________________
Cheers,
Degs

Please post back with your results
CTH Terms of Use

CTH Subscriptions :: Adaware Guide :: HijackThis
Reply With Quote
Reply

Bookmarks

Topic Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 02:54 PM.

[ RSS ]