본문 바로가기

Web/PHP

[PHP] zip에서 이미지 추출하여 미리보기 이미지(thumbnail) 만들기

반응형

출처 : http://blog.bloodcat.com/197

<?php
$source = '/home/user/zip/compressed.zip';
$storage = '/home/user/thumbnails/';
$workspace = '/tmp/thumbnailmaker/';
$path = $storage.'/resized.jpg';

@mkdir($storage, 0777);
@mkdir($workspace, 0777, true);
@unlink($path);

$zip = new ZipArchive();
if($zip->open($source) === true) {
	for($i = 0, $cnt = $zip->numFiles; $i < $cnt; $i++) {
		$fname = $zip->getNameIndex($i);
		if(preg_match('/(jpe?g|gif|png)/', pathinfo($fname, PATHINFO_EXTENSION))) {
			$tpath = $workspace.'/'.sha1(time().rand(0,99));
			copy('zip://'.$source.'#'.$fname, $tpath);

			list($wo, $ho, $ftype) = getimagesize($tpath);
			switch($ftype) {
				case 1: $original = imagecreatefromgif($tpath); break;
				case 2: $original = imagecreatefromjpeg($tpath); break;
				case 3: $original = imagecreatefrompng($tpath); break;
				case 15: $original = imagecreatefromwbmp($tpath); break;
				default: echo 'Can\'t make a thumbnail: '.$ftype; unlink($tpath); break(2);
			}
			unlink($tpath);

			$w = 80; $h = 60;
			$resized = imagecreatetruecolor($w, $h);
			imagecopyresampled($resized, $original, 0, 0, 0, 0, $w, $h, $wo, $ho);
			imagepng($resized, $path, 9);

			chmod($path, 0644);
			break;
		}
	}
}
?>


반응형