메뉴 건너뛰기

프로그램언어

조회 수 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
번호 제목 날짜 조회 수
180 방금 INSERT 했던 SQL 문의 PK(primary key)값 가져오기 2018.07.04 5335
179 PHP 휴대폰번호 짜르기 (preg_replace) "-" 넣기. 형식바꾸기 2018.07.04 4057
178 [이클립스]PHP 개발환경 만들기 file 2018.07.04 7938
177 MySQL(MariaDB) 테이블 만들기 2018.03.28 8152
176 PHP 네이버블로그 원격 글쓰기 API 소스 file 2018.02.09 12465
175 PHP에서 암호화 encrypt 복호화 decrypt 해서 값을 넘기기 2018.02.09 10626
174 base64 인코딩/디코딩 함수의 특징 file 2018.02.09 13078
173 PHP 날짜 함수 2017.04.13 17673
172 PHP, $_SERVER 변수 2017.04.13 19505
171 정규식 2017.04.13 17599
170 파일시스템, 폼 파일업로드 관련 함수 2017.03.27 21681
169 네이버 자동 띄어쓰기를 이용하기 2017.03.27 17940
168 php 엑셀 다운로드 구현 2017.03.07 29782
167 두 날짜 사이의 차이 구하기 2017.03.07 18866
166 날짜 일수 차이 계산 2017.03.07 19977
165 php 세션 유지시간 늘리기 2017.03.07 26315
164 리다이렉션(페이지 이동)의 3가지 방법, location.href 2017.03.07 40127
163 자릿수만큼 앞에 0 붙이기 2017.03.07 19026
162 세션 시작 / 세션 데이터 추가 / 세션 데이터 삭제 / 세션 종료하기 2017.03.07 19102
161 도메인 앞에 자동으로 WWW를 붙이는 방법 2017.03.07 18885
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved