2013-02-26 104 views
3
种植

我使用PHP脚本上传和调整图像大小,很简单:调整图像大小,并用PHP

if($_SERVER["REQUEST_METHOD"] == "POST") { 
    $image = $_FILES["image_upload"]; 
    $uploadedfile = $image['tmp_name']; 

    if ($image) { 
     $filename = stripslashes($_FILES['image_upload']['name']); 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
     if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { 
      $error_txt = 'Immagine incoretta'; 
      $errors=1; 
     } else { 
      $size=filesize($uploadedfile); 
      if ($size > MAX_SIZE*1024) { 
       $error_txt = "Immagine troppo grande"; 
       $errors=1; 
      } 
      if($extension=="jpg" || $extension=="jpeg") { 
       $uploadedfile = $uploadedfile; 
       $src = imagecreatefromjpeg($uploadedfile); 
      } else if($extension=="png") { 
       $uploadedfile = $uploadedfile; 
       $src = imagecreatefrompng($uploadedfile); 
      } else { 
       $src = imagecreatefromgif($uploadedfile); 
      } 

      list($width,$height)=getimagesize($uploadedfile); 

      $newwidth=500; 
      $newheight=375; 
      $tmp=imagecreatetruecolor($newwidth,$newheight); 



      imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 


      $filename = "images/". generateRandomString(5) . $image['name']; 

      imagejpeg($tmp,$filename,100); 

      imagedestroy($src); 
      imagedestroy($tmp); 
     } 
    } 

我想有一个远一点,现在我只是调整图像大小,无论想象中的比例,我想调整它到一个固定的高度,而不会失去原来的比例,这当然是通过裁剪+调整原始图像的大小来实现的。

我不知道如何做到这一点用我的实际imagecreatetruecolorimagecopyresampled功能,并寻找到PHP手册看起来是不是很容易。

有一个非常good library我尝试融入到我的代码,使用它的那样简单mysite.com/lib/timthumb.php?src=castle1.jpg & H = 180 & W = 120但我不知道如何将其与我的实际代码整合。

那么,你有什么建议?

+0

你可以尝试phpthumb库,这是一个成熟的项目,而不是在这里检查演示:http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php请记住,在服务器中的图像处理确实会造成一些问题,如占用CPU,并可能造成严重的瓶颈。 – Panagiotis 2013-02-26 14:09:27

+0

有一个很好的教程,用imagecreatetruecolor url调整大小:http://code.tutsplus.com/tutorials/image-resizing-made-easy-with-php--net-10362将帮助您调整大小而不会失去比例。 – 2014-07-05 12:52:26

回答

2

如果在下面的代码中有任何拼写错误或任何错误,请原谅我。我没有测试过它。我在这里做的是计算高度还是宽度是太长的比例。然后调整来源尺寸以匹配最终的图像尺寸。另外,调整缩小的边的中心,使裁剪的图像居中。

$newwidth = 500; 
    $newheight = 375; 
    $tmp  = imagecreatetruecolor($newwidth, $newheight); 

    $widthProportion = $width/$newwidth; 
    $heightProportion = $height/$newheight; 

    if ($widthProportion > $heightProportion) { 
     // width proportion is greater than height proportion 
     // figure out adjustment we need to make to width 
     $widthAdjustment = ($width * ($widthProportion - $heightProportion)); 

     // Shrink width to proper proportion 
     $width = $width - $widthAdjustment; 

     $x = 0; // No adjusting height position 
     $y = $y + ($widthAdjustment/2); // Center the adjustment 
    } else { 
     // height proportion is greater than width proportion 
     // figure out adjustment we need to make to width 
     $heightAdjustment = ($height * ($heightProportion - $widthProportion)); 

     // Shrink height to proper proportion 
     $height = $height - $heightAdjustment; 

     $x = $x + ($heightAdjustment/2); // Center the ajustment 
     $y = 0; // No adjusting width position 
    } 

    imagecopyresampled($tmp, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height); 

因此,基本上用$ width和$ height变量指定了多少图片(裁剪)。并用$ x,$ y表示我们想要裁剪图片的位置。其余的只是标准的调整大小,以适应全新的形象。

我希望有帮助!

+0

$ x和$ y未定义.. – r3wt 2014-09-01 21:14:02