| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * ²èÁü¥Õ¥¡¥¤¥ë¤ÎÊÑ´¹¤ò¹Ô¤¦ |
|---|
| 4 | */ |
|---|
| 5 | class ImageConverter { |
|---|
| 6 | var $outputImageDir; // ÊÑ´¹¸å¤Î²èÁü¤ÎÊݸÀè |
|---|
| 7 | var $outputImageType; // ÊÑ´¹¸å¤Î²èÁü¤Î·Á¼° |
|---|
| 8 | var $outputImageWidth; // ÊÑ´¹¸å¤Î²èÁü¤Î²£Éý |
|---|
| 9 | var $outputImageHeight; // ÊÑ´¹¸å¤Î²èÁü¤Î¹â¤µ |
|---|
| 10 | var $outputImageFileSize; // ÊÑ´¹¸å¤Î²èÁü¤Î¥Õ¥¡¥¤¥ë¥µ¥¤¥º |
|---|
| 11 | |
|---|
| 12 | // ¥³¥ó¥¹¥È¥é¥¯¥¿ |
|---|
| 13 | function ImageConverter() { |
|---|
| 14 | $this->outputImageDir = realpath(realpath(dirname(__FILE__))); |
|---|
| 15 | $this->outputImageType = 'jpeg'; |
|---|
| 16 | $this->outputImageWidth = 320; |
|---|
| 17 | $this->outputImageHeight = NULL; |
|---|
| 18 | $this->outputFileSize = 20000; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | // ÊÑ´¹¼Â¹Ô |
|---|
| 22 | function execute($inputImagePath) { |
|---|
| 23 | // Á°½èÍý |
|---|
| 24 | $filestat = @stat($inputImagePath); |
|---|
| 25 | $imagesize = getimagesize($inputImagePath); |
|---|
| 26 | $inputImageWidth = $imagesize[0]; |
|---|
| 27 | $inputImageHeight = $imagesize[1]; |
|---|
| 28 | $inputImageType = $imagesize[2]; |
|---|
| 29 | $outputImageName = sha1($inputImagePath . '_' . $this->outputImageWidth . '_' . $this->outputFileSize . '_' . $filestat['mtime']) . '.' . $this->outputImageType; |
|---|
| 30 | $outputImagePath = $this->outputImageDir . '/' . $outputImageName; |
|---|
| 31 | |
|---|
| 32 | // ÊÑ´¹¸å¤Î²èÁü¤Î¹â¤µ¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢ÊÑ´¹¸å¤Î²èÁü¤Î²£Éý¤«¤éµá¤á¤ë |
|---|
| 33 | if (is_null($this->outputImageHeight)) { |
|---|
| 34 | $this->outputImageHeight = $inputImageHeight * ($this->outputImageWidth / $inputImageWidth); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | // ¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¤«³Îǧ¤·¡¢Â¸ºß¤·¤Ê¤¤¾ì¹ç¤Î¤ßºîÀ®¤¹¤ë |
|---|
| 38 | if (file_exists($outputImagePath)) { |
|---|
| 39 | $info['convert'] = FALSE; |
|---|
| 40 | } else { |
|---|
| 41 | // ¸µ¥Õ¥¡¥¤¥ëºîÀ® |
|---|
| 42 | switch($inputImageType) { |
|---|
| 43 | case 1: |
|---|
| 44 | // gif |
|---|
| 45 | $tempImage = imagecreatefromgif($inputImagePath); |
|---|
| 46 | break; |
|---|
| 47 | case 2: |
|---|
| 48 | // jpeg |
|---|
| 49 | $tempImage = imagecreatefromjpeg($inputImagePath); |
|---|
| 50 | break; |
|---|
| 51 | case 3: |
|---|
| 52 | // png |
|---|
| 53 | $tempImage = imagecreatefrompng($inputImagePath); |
|---|
| 54 | break; |
|---|
| 55 | case 6: |
|---|
| 56 | // bmp |
|---|
| 57 | $tempImage = imagecreatefromwbmp($inputImagePath); |
|---|
| 58 | break; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if (!$tempImage) { |
|---|
| 62 | return false; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | $scale = 1.0; |
|---|
| 66 | $outputImagePathTemp = $outputImagePath . '.tmp-' . rand(); |
|---|
| 67 | do { |
|---|
| 68 | // ¶õ¥Õ¥¡¥¤¥ëºîÀ® |
|---|
| 69 | $outputImage = ImageCreateTruecolor($this->outputImageWidth * $scale, $this->outputImageHeight * $scale); |
|---|
| 70 | ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $this->outputImageWidth * $scale, $this->outputImageHeight * $scale, $inputImageWidth, $inputImageHeight); |
|---|
| 71 | |
|---|
| 72 | // ¥Õ¥¡¥¤¥ë½ÐÎÏ |
|---|
| 73 | |
|---|
| 74 | @unlink($outputImagePathTemp); |
|---|
| 75 | |
|---|
| 76 | switch ($this->outputImageType) { |
|---|
| 77 | case 1: |
|---|
| 78 | case 'gif': |
|---|
| 79 | imagegif($outputImage, $outputImagePathTemp); |
|---|
| 80 | break; |
|---|
| 81 | default: |
|---|
| 82 | case 2: |
|---|
| 83 | case 'jpg': |
|---|
| 84 | case 'jpeg': |
|---|
| 85 | $quality = 75; |
|---|
| 86 | // ɽ¼¨²Äǽ¤Ê¥Õ¥¡¥¤¥ë¥µ¥¤¥º°Ê²¼¤Ë¤Ê¤ë¤Þ¤Ç¡¢10%¤º¤Ä¥¯¥ª¥ê¥Æ¥£¤òÄ´À°¤¹¤ë |
|---|
| 87 | do { |
|---|
| 88 | @unlink($outputImagePathTemp); |
|---|
| 89 | imagejpeg($outputImage, $outputImagePathTemp, $quality); |
|---|
| 90 | $quality -= 10; |
|---|
| 91 | clearstatcache(); |
|---|
| 92 | } while (filesize($outputImagePathTemp) > $this->outputFileSize && $quality > 0); |
|---|
| 93 | break; |
|---|
| 94 | case 3: |
|---|
| 95 | case 'png': |
|---|
| 96 | imagepng($outputImage, $outputImagePathTemp); |
|---|
| 97 | break; |
|---|
| 98 | case 6: |
|---|
| 99 | case 'bmp': |
|---|
| 100 | imagewbmp($outputImage, $outputImagePathTemp); |
|---|
| 101 | break; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | // ¥á¥â¥ê³«Êü |
|---|
| 105 | imagedestroy($outputImage); |
|---|
| 106 | |
|---|
| 107 | $scale -= 0.1; |
|---|
| 108 | clearstatcache(); |
|---|
| 109 | } while (filesize($outputImagePathTemp) > $this->outputFileSize && $scale >= 0.5); |
|---|
| 110 | |
|---|
| 111 | rename($outputImagePathTemp, $outputImagePath); |
|---|
| 112 | |
|---|
| 113 | // ¥á¥â¥ê³«Êü |
|---|
| 114 | imagedestroy($tempImage); |
|---|
| 115 | |
|---|
| 116 | $info['convert'] = TRUE; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | $info['outputImagePath'] = $outputImagePath; |
|---|
| 120 | $info['outputImageName'] = $outputImageName; |
|---|
| 121 | return $info; |
|---|
| 122 | |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | // Setter |
|---|
| 126 | function setOutputDir($outputDir) { $this->outputImageDir = $outputDir; } |
|---|
| 127 | function setImageType($imageType) { $this->outputImageType = $imageType; } |
|---|
| 128 | function setImageWidth($imageWidth) { $this->outputImageWidth = $imageWidth; } |
|---|
| 129 | function setFileSize($fileSize) { $this->outputFileSize = $fileSize; } |
|---|
| 130 | // function setImageHeight($imageHeight) { $this->outputImageHeight = $imageHeight; } |
|---|
| 131 | |
|---|
| 132 | // Getter |
|---|
| 133 | function getOutputDir() { return $this->outputDir; } |
|---|
| 134 | function getImageType() { return $this->outputImageType; } |
|---|
| 135 | function getImageWidth() { return $this->outputImageWidth; } |
|---|
| 136 | function getImageHeight() { return $this->outputImageHeight; } |
|---|
| 137 | |
|---|
| 138 | /* |
|---|
| 139 | * PrivateMethod |
|---|
| 140 | */ |
|---|
| 141 | function beforeExecute() { |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | ?> |
|---|