/    Sign up×
Community /Pin to ProfileBookmark

Resizing remote images wayyy slow, help?

Ok, so after tearing my hair out about why my site would sporadically generate SQL errors, I figured out that by removing my remote image gallery, I could stop the problem. I had a few images from flickr on my page that I would resize (using image resize and some math to generate the new dimensions). Even just having one image is causing those problems. I tried a small function that replicates getimagesize without using it (I hear there are some problems), but no luck.

What should I be doing in order to rectify. Here’s my code snippet:

[code=php]
<?
$size = getimagesize($row[‘img’.$i]);
$w = $size[0];
$h = $size[1];
$img[$i] = array(‘image’=>$row[‘img’.$i], ‘w’=>$w, ‘h’=>$h);

function getNewSize($w, $h, $width, $height){
if ($h > $w) {
$new_h = ($width / $w) * $h;
$new_w = $width;
$mTop = ($height/2)-($new_h/2);
$mLeft = 0;
} else {
$new_w = ($height / $h) * $w;
$new_h = $height;
$mTop = 0;
$mLeft = ($width/2)-($new_w/2);
}

return array($new_w, $new_h, $mTop, $mLeft);
}

for($i=1; $i<=3; $i++){
if($img[$i] != “”){
$size = getNewSize($img[$i][‘w’], $img[$i][‘h’], $width, $height);
$big = getNewSize($img[$i][‘w’], $img[$i][‘h’], $bigW, $bigH);
?>
<div class=’image’>
<img src='<?=$img[$i][‘image’]?>’ alt=’Image <?=$i?>’ width='<?=$size[0]?>’ height='<?=$size[1]?>’ style=’margin:<?=$size[2]?>px 0 0 <?=$size[3]?>px’ />
</div>
<?
}
}
?>
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@blue-eye-labsNov 28.2007 — Ok, so after tearing my hair out about why my site would sporadically generate SQL errors, I figured out that by removing my remote image gallery, I could stop the problem. I had a few images from flickr on my page that I would resize (using image resize and some math to generate the new dimensions). Even just having one image is causing those problems. I tried a small function that replicates getimagesize without using it (I hear there are some problems), but no luck.

What should I be doing in order to rectify. Here's my code snippet:

[code=php]
<?
$size = getimagesize($row['img'.$i]);
$w = $size[0];
$h = $size[1];
$img[$i] = array('image'=>$row['img'.$i], 'w'=>$w, 'h'=>$h);

function getNewSize($w, $h, $width, $height){
if ($h > $w) {
$new_h = ($width / $w) * $h;
$new_w = $width;
$mTop = ($height/2)-($new_h/2);
$mLeft = 0;
} else {
$new_w = ($height / $h) * $w;
$new_h = $height;
$mTop = 0;
$mLeft = ($width/2)-($new_w/2);
}

return array($new_w, $new_h, $mTop, $mLeft);
}

for($i=1; $i<=3; $i++){
if($img[$i] != ""){
$size = getNewSize($img[$i]['w'], $img[$i]['h'], $width, $height);
$big = getNewSize($img[$i]['w'], $img[$i]['h'], $bigW, $bigH);
?>
<div class='image'>
<img src='<?=$img[$i]['image']?>' alt='Image <?=$i?>' width='<?=$size[0]?>' height='<?=$size[1]?>' style='margin:<?=$size[2]?>px 0 0 <?=$size[3]?>px' />
</div>
<?
}
}
?>
[/code]
[/QUOTE]


I'll presume that $row contains an actual image string (like a direct link to an image) and I'll try to work out what you're doing from there:

Ok, you need to make the string into an image, you can't just have a string, so for example use:
[code=php]$src = imagecreatefromjpeg($row['img' . $i];[/code]
Only once you've created an image can you manipulate it, I hope that solves your problem, otherwise come back to me on it!
Copy linkTweet thisAlerts:
@calliepeckauthorNov 28.2007 — This is just part of a larger file, I'm not adding an image header to the file (although I could go about it that way too, I suppose). The script works fine with images in my local directories, and it *works* with remote images, it just goes super slow. Basically, I'm trying to figure out how to take a photo from flickr and show it resized on my page. Does it need to look something like:

<img src="image.php?path=http://www.flickr.com/foo" />

?
Copy linkTweet thisAlerts:
@blue-eye-labsNov 28.2007 — oh, I see...

that is going to be slow purely because not only will it take quite a lot of server processing, but the server is trying to grab it from somewhere else, download it and then process it.
Copy linkTweet thisAlerts:
@calliepeckauthorNov 28.2007 — that's what i thought, which is why I had simply put in an img tag like normal:

<img src="http://www.flickr.com/foo" />

And then tried to adjust the size

<img src="http://www.flickr.com/foo" width=x height=y />

where x and y had been generated by getimagesize and then adjusting proportionally. I'm wondering if there's a better way to resize than this.

Bizarrely, flickr gives me more problems than any other image.
Copy linkTweet thisAlerts:
@blue-eye-labsNov 29.2007 — you could [B]just[/B] set either a width or a height, which would be quickest, because it'd require the least amount of processing etc...
×

Success!

Help @calliepeck spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...