메뉴 건너뛰기

프로그램언어

조회 수 20783 추천 수 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;
}
}
?>

  1. No Image 12Apr
    by
    2014/04/12 Views 20473 

    PHP 에서의 소켓(Socket) 통신

  2. No Image 26Mar
    by
    2014/03/26 Views 19577 

    PHP 세션에서 시작, 변수등록, 변수삭제, 부수기

  3. No Image 26Mar
    by
    2014/03/26 Views 17916 

    어떤 스마트폰으로 접속했는지 알아내는 소스 , (안드로이드 아이폰 )

  4. No Image 26Mar
    by 조쉬
    2014/03/26 Views 20783 

    PHP 소스 : 이미지 리사이즈, 섬네일

  5. No Image 26Mar
    by
    2014/03/26 Views 19523 

    네이버 지도 api php버전

  6. No Image 26Mar
    by
    2014/03/26 Views 19604 

    Drag/Drop HTML elements

  7. No Image 26Mar
    by
    2014/03/26 Views 19779 

    이미지 워터마크 구현

  8. No Image 26Mar
    by
    2014/03/26 Views 21719 

    특수문자 없애는 정규표현식

  9. No Image 26Mar
    by
    2014/03/26 Views 19592 

    jQuery 개발자를 위한 메모 - 레퍼런스

  10. No Image 26Mar
    by
    2014/03/26 Views 19135 

    JSON and JavaScript usage

  11. No Image 26Mar
    by
    2014/03/26 Views 19335 

    jQuery 개발자를 위한 메모 - 플러그 인

  12. No Image 26Mar
    by
    2014/03/26 Views 20440 

    웹에서 Excel 로 출력하기

  13. No Image 26Mar
    by
    2014/03/26 Views 19879 

    웹페이지 프린트 하기 html 수준

  14. No Image 26Mar
    by
    2014/03/26 Views 20657 

    [PHP] 한글명 파일 다운로드받기

  15. No Image 27Feb
    by
    2014/02/27 Views 21146 

    FPDF - PHP로 PDF 만들기

  16. No Image 27Feb
    by
    2014/02/27 Views 26435 

    해당하는 날짜가 그달의 몇주째인지 계산

  17. No Image 27Feb
    by
    2014/02/27 Views 19915 

    php 파일 다운로드 구현

  18. No Image 27Feb
    by
    2014/02/27 Views 20303 

    php 파일 확장자

  19. No Image 27Feb
    by
    2014/02/27 Views 19818 

    MYSQL 업데이트 두 번 하기

  20. No Image 27Feb
    by
    2014/02/27 Views 20447 

    mysql 에러 구문 표시

Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved