Current location: Hot Scripts Forums » Programming Languages » PHP » Is this possible cos i'm ripping my hair out...


Is this possible cos i'm ripping my hair out...

Reply
  #1 (permalink)  
Old 03-09-11, 08:36 AM
var's Avatar
var var is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 51
Thanks: 0
Thanked 11 Times in 11 Posts
Angry Is this possible cos i'm ripping my hair out...

Is it possible to resize an image hosted on a seperate site on the fly..

for instance say i have an image on photobucket, can i resize it on the fly and be able to call it like this:

<img src=resize.php?height=x&width=x&src=http://photobucket.com/blah/pic.jpg>

i've been trying alsorts but nothing seems to work.

The reason i need it to work and be called like this is because i am using str_replace and replacing '<img src=' with '<img src=resize.php?height=x&width=x&src=' so that it resizes every image with the resized version.

any ideas where i am going wrong or is this just not possible?

thanks.
Reply With Quote
  #2 (permalink)  
Old 03-09-11, 08:59 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
It is possible, but on must server's it isn't allowed by default.

In the php.ini file (the php config file) there's a setting that allows the reading of files hosted on a different domain, and thus needed to be accessed via the http protocol. This setting, named allow_url_fopen should be turned On

If that doesn't work, you could open a socket to the url of the file, grab the data that is passed through the socket, treat it as an image and do the resizing of it. It's a lot more complex, but will work on any server (unless the socket functions are disabled, which is the case on most free webhosts)
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 03-09-11, 09:36 AM
var's Avatar
var var is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 51
Thanks: 0
Thanked 11 Times in 11 Posts
yes my allow_url_fopen is on, but how will that help?
this all sounds rather complex for what im trying to achieve.

all i wanna do is make a tiny script that takes the image src, checks its size, if its over x wide then shrink it, if not then leave it alone.
Reply With Quote
  #4 (permalink)  
Old 03-09-11, 10:02 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Can you tell us what is wrong with the current code? Does the image show? Are you getting any errors while running the code?
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #5 (permalink)  
Old 03-09-11, 10:23 AM
var's Avatar
var var is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 51
Thanks: 0
Thanked 11 Times in 11 Posts
there are no errors shown, and the image just shows a broken image symbol.

this is the code for imageresize.php:

PHP Code:

<?php


$isrc 
$_GET['isrc'];
$w $_GET['w'];
$h $_GET['h'];

$in getimagesize($isrc);
$sw $in[0] / $w;
$sh $in[1] / $h;
$s  $sw $sh $sw $sh;

$x0 floor( ( $in[0] - ( $w $s ) ) * 0.5 );
$y0 floor( ( $in[1] - ( $h $s ) ) * 0.5 );

$im imagecreatefromjpeg($isrc) or
$im imagecreatefrompng($isrc) or
$im imagecreatefromgif($isrc) or
$im false;

if (!
$im) {
    
readfile($isrc);
} else {
    
$thumb imagecreatetruecolor($w$h);
    
imagecopyresampled($thumb$im00$x0$y0$w$h, ($w $s), ($h $s));
    
imagejpeg($thumb);
}
?>
and i call it like this:

PHP Code:

<img src=imageresize.php?w=200&h=200&isrc=http://photobucket.com/blah/pic.jpg> 

Reply With Quote
  #6 (permalink)  
Old 03-09-11, 02:04 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Looking at your code, I'd say error reporting is set to off, because it should throw at least 1 error. This piece of code
PHP Code:

$im imagecreatefromjpeg($isrc) or
$im imagecreatefrompng($isrc) or
$im imagecreatefromgif($isrc) or
$im false
should throw a syntax error. The correct way to do it is
PHP Code:

$im imagecreatefromjpeg($isrc) or imagecreatefrompng($isrc) or imagecreatefromgif($isrc) or false
But if I'm not mistaken, calling imagecreatefromjpeg on a gif will throw a warning, so you will have to silense the warning using @:
PHP Code:

$im = @imagecreatefromjpeg($isrc) or @imagecreatefrompng($isrc) or @imagecreatefromgif($isrc) or false
Place this small code snippet at the top of the resize.php page, and see if any error is outputted:
PHP Code:

ini_set('display_errors''On');
error_reporting (E_ALL E_STRICT); 
The errors probably won't show when you call the script through the <img src=""> tag. Try opening the page in your browser by passing on a url.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #7 (permalink)  
Old 03-11-11, 03:42 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
His code looks actually right to me! Your's will run through the different functions, but it won't assign $im unless it's an JPG.
Reply With Quote
  #8 (permalink)  
Old 03-11-11, 04:47 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Damn, didn't know that. I'd better start checking all my code.
I always assumed the returned value would be assigned to the variable
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #9 (permalink)  
Old 03-11-11, 05:36 AM
var's Avatar
var var is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 51
Thanks: 0
Thanked 11 Times in 11 Posts
so any idea why its not working?
could it be my php version, phpinfo says "PHP Version 4.4.9"
Reply With Quote
  #10 (permalink)  
Old 03-11-11, 06:12 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Why are you still on PHP 4?!?!?! Anything below PHP 5.3 is not officially supported anymore. If you're not on a VPS or private host, ask your provider to update!

Then, take a look at what UnrealEd says:
Quote:
Originally Posted by UnrealEd View Post
It is possible, but on must server's it isn't allowed by default.
In the php.ini file (the php config file) there's a setting that allows the reading of files hosted on a different domain, and thus needed to be accessed via the http protocol. This setting, named allow_url_fopen should be turned On
If this isn't enabled, it won't work.

Try this as well:
Quote:
Originally Posted by UnrealEd
Place this small code snippet at the top of the resize.php page, and see if any error is outputted:
PHP Code:

ini_set('display_errors''On');
error_reporting (E_ALL E_STRICT); 
People are trying to help, but you have to listen and try the suggestions. Otherwise we won't be able to help.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 08:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.