Jump to content

I have a problem referencing pics on remote servers... they're too big!


Tuskony

Recommended Posts

Hey guys....

 

I have a website that allows uses to log in and create trucks to share with the community. However I don't want to have to store hundreds of images on my home based server (for obvious reasons) so I let users link to pictures on other sites... like they're photobucket account for example.

 

The users can also choose one of the pictures to be their thumbnail picture for that particular truck. However by referencing to the pictures on the remote sites I have a problem with displaying the images. Because the thumbnail pics are shown so much it bogs the pages down (even though I resize the pictures to 50x50 display) because the pictures on the remote site are large in size.

 

Is there anyway I can download and resize then save all thumbnail pictures onto my machine so they're not so big and make loading quicker?

 

Or is there a better way? Also is there anyway for me to have a script that can measure the pictures on the foriegn site? When someone is viewing a full sized picture i want to know if the picture is too tall or too wide an needs to be resized.

 

Thanks!

<?php
class image{

function set_createthumb($name,$filename,$new_w,$new_h)
{
$system=explode('.',$name);
if (preg_match('/jpg|jpeg/',$system[1]))
{
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png/',$system[1]))
{
$src_img=imagecreatefrompng($name);
} 
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{ $thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w; $thumb_h=$new_h;
} 
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
}else{
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

function set_image_pro($image,$type,$size,$tmp,$query,$url)
{
define ("MAX_SIZE","500");

if ((($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg"))
&& ($size < MAX_SIZE*1024))
  {
  if ($_FILES["image"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["image"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $image . "<br />";
    echo "Type: " . $type . "<br />";
    echo "Size: " . ($size / 1024) . " Kb<br />";

if (file_exists("images/origimg/" . $image))
      {
      echo $image . " already exists. ";
      }
    else
      {
  move_uploaded_file($tmp,"images/origimg/".$image);
  $origim="images/origimg/".$image;
  $thumbim="images/".$image;
  $thumbw="400";
  $thumbh="400";
  $this->set_createthumb($origim,$thumbim,$thumbw,$thumbh);
  $origim="images/".$image;
  $thumbim="images/thumbs/".$image;
  $thumbw="150";
  $thumbh="150";
  $this->set_createthumb($origim,$thumbim,$thumbw,$thumbh);
      echo "Stored in: " . "images/" . $image;
  echo "<h1>Uploaded Successfully</h1>";
  $sql=mysql_query($query);
  echo"<META HTTP-EQUIV=\"Refresh\" Content=\"2;URL=$url\">\n";
      }
    }
  }
else
  {
  echo "Invalid file";
  echo "Type: " . $type;
  }
}

}
?>

 

With some modification this script should create the thumbnails for you. The top function is all you're really interested in. However, the bottom function takes an image, resizes it to a large but not massive size, saves that as the large image on the server, then creates the thumbnail from the same image etc... This may solve all of your resizing problems.

 

This class includes all of the functions you should need for having images from other servers resized on the fly too, it'll just take some thinking...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.