What is the most efficient way to store profile images for a social networking site?

I created a simple social network and gave users the ability to upload profile photos. The fact is that I configured it so that when loading the image was changed to two sizes, which are used in my site design. However, the customer needs a design change after some time. When developing a new design for images, a different size is required. This is similar to how after 2 years of operation of the site, and we have a large number of users with profile images.

At this moment, I think that I made a mistake when resizing images to fixed sizes, since now I will need to provide for the resizing of all images that are already installed in the system with a new design. Why the question arises: what is the way to save images on the server, which will be displayed on different sizes?

+3
source share
4 answers

If you want to save the problem of resizing them all at the same time, you may have a condition that checks whether it has changed before the new size, and then resize it the first time you access the image.

  $edit_file = $_edit_id.'_'.basename($_FILES['edit_imagename']['name']);
  $uploadfile = $orig_uploaddir . $edit_file;
  if (move_uploaded_file($_FILES['edit_imagename']['tmp_name'], $uploadfile)) 
    {
    //---resize image to regular and thumbnail-----------------------------------b
    $src = imagecreatefromjpeg($uploadfile);
    $image_info=getimagesize($uploadfile);
    list($width,$height)=$image_info;
    //---create 400x400 fullsize--------------b
    $newwidth=400;
    $newheight=round(($height/$width)*$newwidth);
    if ($newheight>400)
      {
      $newheight=400;
      $newwidth=round(($width/$height)*$newheight);
      }
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $filename = $uploaddir . $_edit_id.'_400_'.basename($_FILES['edit_imagename']['name']);
    imagejpeg($tmp,$filename,100);
    //---create 400x400 fullsize--------------e
    //---create 200x200 thumbnail--------------b
    $tn_width=200;
    $tn_height=round(($height/$width)*$tn_width);
    if ($tn_height>200)
      {
      $tn_height=200;
      $tn_width=round(($width/$height)*$tn_height);
      }
    $tmp=imagecreatetruecolor($tn_width,$tn_height);
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
    $filename = $uploaddir . $_edit_id.'_200_'.basename($_FILES['edit_imagename']['name']);
    imagejpeg($tmp,$filename,100);
    //---create 200x200 thumbnail--------------e
    imagedestroy($tmp); 
0
source

( , ), .: -)

, php GD .

+1

, - , - ... .

, , , .

0

My best bet is to create a thumbnail script and resize the photos on the fly so you can store more photos. This will reduce the use of your hard drive by 50%; but it will add a few milliseconds to the loading of your page.

-1
source

Source: https://habr.com/ru/post/1756703/


All Articles