개발! 딱 깔끔하고 센스있게!

그림 크기 조정 PHP[반복] 본문

개발 스크랩 메모/PHP

그림 크기 조정 PHP[반복]

렉사이 2020. 12. 28. 23:54

Possible Duplicate:
Can anybody suggest the best image resize script in php?

PHP 의 그림 처리나 파일 처리에 대해서는 아직 초보자입니다.

다음 정보 제공

나는 간단한 html 폼으로 그림 파일을 발표하고 php 을 통해 올린다.

내가 더 큰 파일을 수용할 때, 내 코드를 수정하려고 시도했을 때, 나는 오류를 얻었다.

인터넷에서 계속 검색했지만 진정한 간단한 것을 찾지 못했다.

$size = getimagesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size == FALSE) { $errors=1; }else if($size[0] > 300){ //if width greater than 300px $aspectRatio = 300 / $size[0]; $newWidth = round($aspectRatio * $size[0]); $newHeight = round($aspectRatio * $size[1]); $imgHolder = imagecreatetruecolor($newWidth,$newHeight); } $newname= ROOTPATH.LOCALDIR."/images/".$image_name; //image_name is generated $copy = imagecopyresized($imgHolder, $_FILES['image']['tmp_name'], 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]); move_uploaded_file($copy, $newname); //where I want to move the file to the location of $newname 

내가 얻은 잘못은:

imagecopyresized(): supplied argument is not a valid Image resource in

미리 감사합니다.


당신의 의견에 감사드립니다.

저는 이것으로 바꿨습니다.

$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name'])); $copy = imagecopyresized($imgHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]); if(!move_uploaded_file($copy, $newname)){ $errors=1; } 

PHP 로그를 가져오지 않았지만 저장되지 않았습니다: (()

무슨 생각 있으세요?

다시 한 번 감사 드립니다


결과

다음 작업.

$oldImage = imagecreatefromjpeg($img); $imageHolder = imagecreatetruecolor($newWidth, $newHeight); imagecopyresized($imageHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagejpeg($imageHolder, $newname, 100); 

도와주셔서 감사합니다.

대답

imagecopyresized 는 그림 자원을 두 번째 인자로서 파일 이름이 아닙니다.

파일을 먼저 불러야 합니다.

파일 형식을 알면 immagecreatefromFILETYPE를 사용하여 불러올 수 있습니다.

예를 들어 JPEG 라면 immagecreatefromjpeg을 사용하여 파일명을 전달합니다.

-이것은 그림 자원으로 되돌아갑니다.

파일 형식을 모르면 모든 파일을 잃어버리지 않습니다.

파일을 문자열로 읽을 수 있으며, immagecreatefromstring (자동으로 파일 형식) 을 불러올 수 있습니다.

$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name'])); 
Comments