Click to See Complete Forum and Search --> : Resizing remote images wayyy slow, help?
calliepeck
11-28-2007, 11:11 AM
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:
<?
$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>
<?
}
}
?>
blue-eye-labs
11-28-2007, 11:35 AM
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:
<?
$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>
<?
}
}
?>
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:
$src = imagecreatefromjpeg($row['img' . $i];
Only once you've created an image can you manipulate it, I hope that solves your problem, otherwise come back to me on it!
calliepeck
11-28-2007, 12:37 PM
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" />
?
blue-eye-labs
11-28-2007, 01:51 PM
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.
calliepeck
11-28-2007, 04:33 PM
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.
blue-eye-labs
11-29-2007, 12:07 PM
you could just set either a width or a height, which would be quickest, because it'd require the least amount of processing etc...