Last active
August 29, 2015 13:56
-
-
Save desbo/636b2bd63d5bffd82b4a to your computer and use it in GitHub Desktop.
bad photos of phil hill script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DogImage { | |
public $filename; | |
public $gdImage; | |
public $mimeType; | |
public $targetWidth = 960; | |
public $allowedTypes = array( | |
1, // [] gif | |
2, // [] jpg | |
3, // [] png | |
6 // [] bmp | |
); | |
public function __construct($filename) { | |
$this->filename = $filename; | |
$this->imageMimeType = mime_content_type($filename); | |
} | |
public function getGdImage() { | |
$type = exif_imagetype($this->filename); | |
if (in_array($type, $this->allowedTypes)) { | |
switch ($type) { | |
case 1 : | |
$this->gdImage = imageCreateFromGif($this->filename); | |
break; | |
case 2 : | |
$this->gdImage = imageCreateFromJpeg($this->filename); | |
break; | |
case 3 : | |
$this->gdImage = imageCreateFromPng($this->filename); | |
break; | |
case 6 : | |
$this->gdImage = imageCreateFromBmp($this->filename); | |
break; | |
} | |
} | |
return $this->_resize(); | |
} | |
public function getMimeType() { | |
return $this->imageMimeType; | |
} | |
protected function _resize() { | |
$origWidth = imagesx($this->gdImage); | |
$origHeight = imagesy($this->gdImage); | |
$targetHeight = (($origHeight * $this->targetWidth) / $origWidth); | |
$resizedImage = imagecreatetruecolor($this->targetWidth, $targetHeight); | |
imagecopyresized($resizedImage, $this->gdImage, | |
0, 0, 0, 0, | |
$this->targetWidth, $targetHeight, | |
$origWidth, $origHeight); | |
return $resizedImage; | |
} | |
} | |
$image_types = array( | |
'gif' => 'image/gif', | |
'png' => 'image/png', | |
'jpg' => 'image/jpeg', | |
); | |
$folder = 'images'; | |
$images = array(); | |
foreach (scandir($folder) as $node) { | |
$path = $folder . '/' . $node; | |
if (!is_dir($node) && in_array(mime_content_type($path), $image_types)) { | |
$images[] = $path; | |
} | |
} | |
shuffle($images); | |
$imagePath = $images[0]; | |
$imageFile = basename($imagePath); | |
$resized = './images/resized/' . $imageFile; | |
if (!file_exists($resized)) { | |
$dog = new DogImage($images[0]); | |
imagejpeg($dog->getGdImage(), $resized); | |
} | |
$dog = fopen($resized, 'rb'); | |
header('Content-Type: image/jpeg'); | |
header('Content-Length: ' . filesize($resized)); | |
fpassthru($dog); | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment