메뉴 건너뛰기

프로그램언어

조회 수 20704 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
<?php
// resize-all-photos.php
//
// A PHP5 script to resize a batch of photos stored in a db.
//
// by pb, http://www.onfocus.com/
//
// Use at your own risk.

set_time_limit(48000);

//Application Settings
define("PHOTO_MAX_WIDTH",850); // Maximum size a photo should be
define("PHOTO_MAX_HEIGHT",640); // Maximum size a photo should be
define("PHOTO_QUALITY",95); // Quality for photo resizing
define("SALT","[your unique salt]"); // Helps with file naming

//Add your local photos folders (with trailing slashes)
$photodir = "[full path to photo directory]";
$thumbdir = "[full path to thumbs directory]";

//Add your MySQL details
$mysql_server = "localhost";
$mysql_user = "[user]";
$mysql_pass = "[password]";
$mysql_db = "[db name]";

//Get this db started
if (!$connection = @ mysql_connect($mysql_server, $mysql_user, $mysql_pass))
die("Can't connect to the database!");
if (!mysql_select_db($mysql_db, $connection))
die("Error " . mysql_errno() . " : " . mysql_error());

//Grab the PhotoID and File Location of all photos
$query = "SELECT PhotoID, File, DateCreated FROM Photos";
if (!$result = @ mysql_query ($query, $connection))
printMySQLerror();
if (mysql_num_rows($result) == 0) {
die("Couldn't find any photos!");
}
else {
while ($photo = mysql_fetch_array($result)) {
$photoID = $photo["PhotoID"];
$photoFile = $photo["File"];
$photoDate = $photo["DateCreated"];
$photoYear = date("Y",strtotime($photoDate));
$thumbYearDir = $thumbdir . $photoYear;
list($width, $height, $type, $attr) = @getimagesize($photoFile);

//Check to see if thumbs directory exists
if (!is_dir($thumbYearDir)) {
mkdir($thumbYearDir, 0777, true);
}

//See if original image is bigger than the max image size
if ($width > PHOTO_MAX_WIDTH) {
//Copy the original to [file]_o.jpg if necessary
$newPhotoFile = str_replace(".jpg", "_o.jpg", $photoFile);
if (!file_exists($newPhotoFile)) {
if (!copy($photoFile, $newPhotoFile)) {
print "Error: couldn't copy $photoFile.<br />";
}
}
//Resize
$newheight = Round($height * PHOTO_MAX_WIDTH) / $width;
if (resizePhoto($photoFile,PHOTO_MAX_WIDTH,$newheight,$photoFile,false)) {
print "$photoFile resized.<br />";
}
}
elseif ($height > PHOTO_MAX_HEIGHT) {
//Copy the original to [file]_o.jpg if necessary
$newPhotoFile = str_replace(".jpg", "_o.jpg", $photoFile);
if (!file_exists($newPhotoFile)) {
if (!copy($photoFile, $newPhotoFile)) {
print "Error: couldn't copy $photoFile.<br />";
}
}
$newwidth = Round($width * PHOTO_MAX_HEIGHT) / $height;
if (resizePhoto($photoFile,$newwidth,PHOTO_MAX_HEIGHT,$photoFile,false)) {
print "$photoFile resized.<br />";
}
}

//Set up the base file name for thumbnails
$thumbBaseFile = $thumbYearDir . "\" . md5(SALT.$photoID);

//See if square thumb file is needed (85x85)
$thumbFile_s = $thumbBaseFile . "_s.jpg";
if (!file_exists($thumbFile_s)) {
if (resizePhoto($photoFile,85,85,$thumbFile_s,true)) {
print "$thumbFile_s created.<br />";
}
}

//See if tiny thumb file is needed (100 max)
$thumbFile_t = $thumbBaseFile . "_t.jpg";
if (!file_exists($thumbFile_t)) {
if ($width > $height) {
$newheight = Round($height * 100) / $width;
if (resizePhoto($photoFile,100,$newheight,$thumbFile_t,false)) {
print "$thumbFile_t created.<br />";
}
}
else {
$newwidth = Round($width * 100) / $height;
if (resizePhoto($photoFile,$newwidth,100,$thumbFile_t,false)) {
print "$thumbFile_t created.<br />";
}
}
}

//See if medium thumb file is needed (240 max)
$thumbFile_m = $thumbBaseFile . "_m.jpg";
if (!file_exists($thumbFile_m)) {
if ($width > $height) {
$newheight = Round($height * 240) / $width;
if (resizePhoto($photoFile,240,$newheight,$thumbFile_m,false)) {
print "$thumbFile_m created.<br />";
}
}
else {
$newwidth = Round($width * 240) / $height;
if (resizePhoto($photoFile,$newwidth,240,$thumbFile_m,false)) {
print "$thumbFile_m created.<br />";
}
}
}
$cnt++;
if ($cnt == 10) {
sleep(2);
$cnt = 0;
}
flush();
ob_flush();
}
}

//thanks for the help ZenPhoto, http://www.zenphoto.org/
//and fluffle, http://us2.php.net/manual/en/function.imagecopyresampled.php#53031

function resizePhoto($original,$width,$height,$destination,$crop) {
if ($originalImage = @imagecreatefromjpeg($original)) {
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);
$newImage = imagecreatetruecolor($width, $height);
if ($crop) {
if ($originalWidth > $originalHeight) {
$offsetWidth = ($originalWidth-$originalHeight)/2;
$offsetHeight = 0;
$originalWidth = $originalHeight;
} elseif ($originalHeight > $originalWidth) {
$offsetWidth = 0;
$offsetHeight = ($originalHeight-$originalWidth)/2;
$originalHeight = $originalWidth;
} else {
$offsetWidth = 0;
$offsetHeight = 0;
}
imagecopyresampled($newImage, $originalImage, 0, 0, $offsetWidth, $offsetHeight, $width, $height, $originalWidth, $originalHeight);
}
else {
imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
}

//Create the image file
touch($destination);
imagejpeg($newImage, $destination, PHOTO_QUALITY);
chmod($destination, 0644);
imagedestroy($newImage);
imagedestroy($originalImage);
return true;
}
else {
print "Couldn't load file: $original";
return false;
}
}
?>

List of Articles
번호 제목 날짜 조회 수
220 내 계정 용량 체크 2019.01.08 1595
219 사업자번호로 사업자 종류알기 2019.01.08 1222
218 디비내용을 엑셀 파일로 다운로드 시키는 방법 2019.01.08 1391
217 php/asp에서 가상번호 부여와 가상번호를 거꾸로 적용 2019.01.08 1429
216 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1803
215 배열을 테이블로 만들기 2019.01.08 1624
214 include 와 namespace 2019.01.08 1087
213 파일 2019.01.08 1226
212 디렉토리의 제어 2019.01.08 1214
211 PHP 문자열에서 검색어를 기준으로 앞뒤로 일정 길이만큼 자르기 2018.10.27 3539
210 PHP 랜덤확률 구하기 2018.10.27 4761
209 PHP 소켓을 이용하여 URL의 응답결과를 문자열로 받기 2018.10.27 3502
208 PHP 랜덤 문자열 생성 2018.10.27 4121
207 PHP XML 문서파싱 (SAX 방식 , DOM 방식) file 2018.10.27 3585
206 PHP split()와 explode()의 차이점 2018.10.27 3536
205 PHP eregi가 빠를까, strpos가 빠를까? 2018.10.27 4091
204 PHP 확장 모듈을 이용한 C 라이브러리 사용 2018.10.27 3785
203 자바스크립트 이스케이프 문자열을 PHP로 디코딩 하기 2018.10.27 3259
202 이미지 땡겨와서 출력하기 2018.09.28 5284
201 DB 연동 4단 셀렉트 박스 2018.09.28 6070
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved