2009-11-11 2 views
1

getimagesize()를 사용하여 jpg, gif, png 또는 다른 유형의 이미지를 검사하는 PHP 코드를 어떻게 만들 수 있는지 궁금합니다.PHP를 사용하여 이미지 크기 조정 질문이 있으십니까?

그런 다음 이미지 CSS의 폭이 다음 CSS 코드의 너비보다 작은 지 확인하십시오. 그렇다면 PHP 코드는 가로 또는 세로의 이미지 종횡비를 유지하면서 이미지의 크기를 다시 조정해야합니다. .

나는 그것이 getimagesize()를 사용하는 것 같지만는 이미지 크기 다시하지 않는 것보다 코드의이 작품은 온라인으로 나열 발견

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

CSS 코드입니다. 이 문제를 해결할 수있는 방법이 있습니까?

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

답변

1

CSS를 사용하여 이미지의 크기를 설정할 수도 있습니다. 어느 쪽이든, 이미지를 확대 할 때 이미지의 품질이 저하됩니다.

+0

그렇게 다시하지 않을 경우 이미지 폭이 100 픽셀보다 작은 경우 확인하고 참조하는 방법을 내가 할 수있는 알고 -크기. – seeu

+0

죄송합니다. – seeu

+0

영화 * 줌과 같이 작동하는 경우에만! 높이다! z 방향으로 90도 회전! * – alex

1

PHP ImageMagick Library's resizeImage method을 사용하거나 더 나은 것은 scaleImage method 일 수 있습니다. 내 이해가 정확하다면 문자 그대로 이미지의 크기를 조정하려고하는 것입니다.

프리젠 테이션 단계에서 크기를 조정하는 것에 대해 이야기하는 경우 this article이 이에 대해 언급하는 것 같습니다. getimagesize을 사용하여 치수를 가져온 다음 그에 따라 img 태그에 전달하는 문자열을 반환합니다. 이게 너에게 쓸모가있을 거라 확신한다.

+0

위의 코드를 읽었을 때 그 코드를 읽었습니다. 그러나 그 코드를 가지고 놀았지만 어떤 이유로 이미지의 크기를 재 지정하지 않습니다? – seeu

+0

바퀴를 다시 열지 마십시오. ImageMagick을 사용하십시오! –

0

서버에 ImageMagick 또는 gd가 설치되어 있는지 또는 다른 PHP 이미지 라이브러리가 여러 개 있는지 확인할 수 있습니다.

아마도 더 나은 또 다른 옵션은 자바 스크립트에서 이미지 조작을 수행하는 것입니다.

+0

클라이언트 쪽 조작은 서버 쪽 이미지 조작보다 더 좋은 옵션이 아닙니다. –

+0

그는 이미지를 조작하지 않고, 크기를 얻고 표시 목적에 따라 CSS를 설정합니다. 당신이 그 질문을 읽으면 당신은 그것을 얻었을 것입니다. – Myles

1

귀하의 질문은 이미지가 특정 너비보다 큰지 여부를 확인하여 CSS/HTML을 사용하여 이미지의 크기를 조정하지 않는 방법을 알려주는 것입니다.

서버를 사용하면 너무 큰 이미지를 비례 축소 할 수 있습니다. 브라우저에서 크기를 조정할 필요가없는 것보다 서버가 항상이 작업을 수행한다고 신뢰할 수 있다면.

1

이미지 크기 조정의 기본 사항으로 Simon Jarvis의 코드를 사용하는 사내 프레임 워크 용 사용자 지정 Image 클래스를 만들었습니다.

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

은 기본적으로 당신이 원하는 무엇을위한 마법 나는 우리의 이미지 클래스 (모든 클래스 속성에서 사용하는 다음과 같은 방법으로 온다 :

당신은 샘플 코드 여기 자습서를 찾을 수 있습니다 다른 곳에서 설정되지만, 당신은 하드 코딩이 값은 당신이) 필요 수있는 경우 :

내가 원하는
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); 
    } 
}