【问题标题】:WHats the most efficient way to store profile images for a social networking website?为社交网站存储个人资料图像的最有效方法是什么?
【发布时间】:2010-07-31 18:34:23
【问题描述】:

我建立了一个简单的社交网络,并让用户能够上传个人资料照片。问题是我已经设置了它,以便在上传图片时将图片调整为我的网站设计中使用的两种尺寸。然而,客户需要在一段时间后更改设计。新的设计思想需要不同的图像尺寸。这就像网站运营了 2 年后,我们拥有大量拥有个人资料图片的用户。

在这一点上,我认为我在将图像大小调整为固定大小时犯了一个错误,因为现在我必须为调整系统上已有的所有图像的大小以使用新设计进行设置。这引出了一个问题——在服务器上维护以不同尺寸查看的图像的最佳方式是什么?

【问题讨论】:

    标签: php image-processing


    【解决方案1】:

    根据图像的绝对数量(如果太多,这可能不实用),我会在一个永久位置保留原始图像,并将调整大小的图像保存在不同的缓存位置以供下次使用您的客户决定更改规格的时间。 :-)

    如果空间有限并且您不关心缓存调整大小的图像,您可以使用 php 的 GD 库即时调整它们的大小。

    【讨论】:

      【解决方案2】:

      如果您想省去一次调整所有大小的麻烦,您可以有一个条件来检查其是否已调整为新大小,然后在第一次访问图像时调整它的大小。

        $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); 
      

      【讨论】:

        【解决方案3】:

        我认为没有任何社交网络会在整个设计更改过程中更改其图像大小......这是有充分理由的。

        我想您可以保留图像的最高分辨率版本并从中批处理新图像的负载,将图像尺寸作为文件名的一部分,以便可以轻松地对它们进行分组。

        【讨论】:

        • 这里的一般良好做法(如果您可以根据您的用户群大小负担得起存储空间)是保留比您希望在网站上实际显示的更大的“原始”版本,因此如果以后由于重新设计而需要,您可以重新处理。
        【解决方案4】:

        我发现最好创建一个缩略图脚本并即时调整照片大小,这样您就可以存储更多图片。它将减少 50% 的 HDD 使用量;但它会为您的页面加载增加几毫秒的时间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-05
          • 1970-01-01
          • 1970-01-01
          • 2012-01-17
          • 2012-07-17
          • 2011-06-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多