메뉴 건너뛰기

프로그램언어

조회 수 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
번호 제목 날짜 조회 수
280 DB 내용을 화면에 출력(이중 for 문) 2015.04.14 21246
279 POST값 통째로 인코딩하기 1 2015.04.06 21175
278 체크박스, post 로 넘기고 받아서 다시 체크하기, checkbox 2017.03.07 21127
277 PHP continue 문 file 2015.04.14 21097
276 FPDF - PHP로 PDF 만들기 2014.02.27 21073
275 RSSReader Class 제작 및 Reader 만들기 file 2016.08.22 21042
274 Ajax로 구연한 실시간 서버시간출력 file 2017.03.06 21031
273 PHP switch 문 file 2015.04.14 20992
272 PHP error 메시지 출력 file 2015.04.14 20924
271 preg_match (정규표현식 매치를 수행합니다) 2016.12.23 20843
270 검색어 처리 루틴 2015.04.14 20778
269 array_slice 배열의 일부를 추출 2016.12.23 20775
268 템플릿 관련 정보 2016.08.22 20732
» PHP 소스 : 이미지 리사이즈, 섬네일 2014.03.26 20704
266 HTTP Protocol의 data method - GET / POST 2016.04.22 20637
265 [PHP] 한글명 파일 다운로드받기 2014.03.26 20551
264 문자열 치환하기 2015.04.14 20492
263 stripslashes — 따옴표 처리한 문자열을 풉니다 2016.12.23 20486
262 quotemeta 모든 메타 문자앞에 역슬래쉬를 붙인 문자열을 반환 2016.12.23 20461
261 stripcslashes — addcslashes()로 인용한 문자열을 되돌림 2016.12.23 20442
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved