Görüntü php kullanarak soruyu yeniden boyutlandırma?

5 Cevap php

Ben getimagesize kullanarak bir jpg, gif, png veya görüntü dışındaki diğer türleri () kontrol edecek bir PHP kodu bir parça oluşturmak nasıl merak edildi.

Böylece daha sonra PHP kodu yeniden boy görüntü görüntüleri boy oranını olursa olsun, eğer tutmak gerekir iken bütün halinde görüntüleri genişliği aşağıdaki aşağıdaki CSS kodunu genişliği daha küçük olup olmadığını kontrol edin ve daha sonra onun manzara ya da portre.

CSS kodu

overflow: hidden;  width:100px; height: auto;

Ben online bu aşağıda listelenen bu kod parçası () getimagesize kullanıyor gibi görünüyor bulundu ama yeniden boy resim görünmüyor. Ben bunu düzeltmek için bir yolu var mı.

<?php

function imageResize($width, $height, $target) {

//takes the larger size of the width and height and applies the  
//formula accordingly...this is so this script will work  
//dynamically with any size image

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);

//returns the new sizes in html image tag format...this is so you
//can plug this function inside an image tag and just get the

return "width=\"$width\" height=\"$height\"";

}

?>

<?php

//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");

?>

<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <?php imageResize($mysock[0],  
$mysock[1], 150); ?>>

5 Cevap

Siz de sadece CSS kullanarak görüntünün boyutunu ayarlamak olabilir. Her iki durumda da, görüntüyü büyütmek zaman görüntü kalitesi düşer.

Sen PHP ImageMagick Library's resizeImage method kullanmayı deneyin, ya da belki daha iyisi scaleImage method olabilir. Benim anlayışım doğru ise bu tam anlamıyla bir görüntüyü yeniden boyutlandırmak istediğiniz olmasıdır.

Eğer sadece sunum aşamasında üzerinde boyutlandırma bahsediyoruz, o zaman this article Bu konuda konuşmak gibi görünüyor. Bu getimagesize kullanarak boyutlarını alır ve daha sonra buna göre img etiketi geçmek bir dize döndürür. Ben bu sizin için bazı kullanım olurdu eminim.

Sizin soru boyutlandırmak-up bu görüntüleri CSS / HTML kullanmak için değil biliyorum ki bir görüntü, belli bir genişlikten daha büyük ise sunucu sormak için nasıl.

Daha iyi bir yaklaşım oransal çok büyük olan herhangi bir resmi küçültmeyi sunucuyu kullanmak olabilir. Eğer tarayıcıda herhangi bir boyutlandırma yapmak gerekmez daha sunucu her zaman, bunu yapacak güvenebilirsiniz eğer.

Ben resim boyutlandırma temelleri için Simon Jarvis kodu kullanır bizim in-house çerçeve için özel bir Image sınıfı inşa.

Sen örnek kod ve burada bir öğretici bulabilirsiniz:

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

Temelde yapmak istediğiniz için sihirli bizim görüntü sınıfta kullanabileceğiniz aşağıdaki yöntemlerden aşağı gelir (Herhangi sınıfı özellikleri başka bir yerde ayarlanır, ancak bu değerler gerekiyorsa, sadece sabit kod can):

public function resize($width,$height) 
{
  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->_image = $new_image;

  unset($new_image);

  return $this;   
}

public function resizeToHeight($height) 
{
  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);

  return $this;
}

public function resizeToWidth($width) 
{
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);

  return $this;
}

public function output()
{	
	if($this->_image) {
		ob_clean();
		ob_end_clean();

	    // Start sending headers
	    header("Pragma: public"); // required
	    header("Expires: 0");
	    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	    header("Cache-Control: private",false); // required for certain browsers
	    header("Content-Transfer-Encoding: binary");
	    header("Content-Type: " . $this->_settings['mime']);

		$function = 'image' . substr($this->_settings['mime'], 6); 
	    $function($this->_image);
	}	
}

Eğer sunucu ImageMagick varsa veya yüklü gd görmek, ya da diğer PHP resim kitaplıkları herhangi bir sayı olabilir.

Muhtemelen daha iyi başka bir seçenek, javascript görüntü manipülasyon yapmaktır.