Zoeken naar kunst
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
class ArtOliveImage {
private static $seed = 'bladieblfgl23lsd923';
private static $originalLocation = '/../public_html/img/works/originals/';
private static $originalLocation2 = '/../public_html/img/works/large/';
private static $originalArtistLoc = '/../public_html/img/works/artists/';
private static $newLocation = '/../public_html/kunstimg/';
private static $emptyImage = '/../public_html/img2011/site/empty.gif';
private static $jpegQuality = 90;
static function getURL($id, $xsize = 100, $ysize = 100, $xtype = 'max', $ytype = 'max', $name = 'image.jpg') {
return '/image.php?id=' . $id . '&hash=' . self::getHash($id, $xsize, $ysize, $xtype, $ytype) . '&xsize=' . $xsize . '&ysize=' . $ysize . '&xtype=' . $xtype . '&ytype=' . $ytype . '&name=' . $name;
return '/kunst/img/' . $id . '/' . self::getHash($id, $xsize, $ysize, $xtype, $ytype) . '-' . $xsize . '-' . $ysize . '-' . $xtype . '-' . $ytype . '/' . $name;
}
// static function getNoImageURL($xsize = 100, $ysize = 100, $xtype = 'max', $ytype = 'max') {
// return "img/0/" . self::getHash(0, $xsize, $ysize, $xtype, $ytype) . '-' . $xsize . '-' . $ysize . '-' . $xtype . '-' . $ytype . '/' . 'empty.jpg';
// }
static function getNewDimensions($id, $xsize, $ysize, $xtype, $ytype){
$original = self::getOriginal($id);
if(false == $original){
return list($width, $height) = @getimagesize(self::$emptyImage);
}
list($width, $height) = @getimagesize($original);
$width = ($width == 0) ? 0.01 : $width;
$height = ($height == 0) ? 0.01 : $height;
$newSize = self::getNewSize($width, $height, $xsize, $ysize, $xtype, $ytype);
return $newSize;
}
static function getOriginalDimensions($id){
$original = self::getOriginal($id);
if(false == $original){ $original = self::$emptyImage; }
list($width, $height) = @getimagesize($original);
return array('width'=>$width,'height'=>$height);
}
static function getResizedURL($id, $xsize, $ysize, $xtype, $ytype, $hash) {
if (!self::checkHash($id, $xsize, $ysize, $xtype, $ytype, $hash)) Throw new Exception('Invalid Hash: ' . self::getHash($id, $xsize, $ysize, $xtype, $ytype));
$original = self::getOriginal($id);
if(false == $original){ return self::$emptyImage; }
$loc = self::getNewLocation();
$file = $loc . $id . '-' . $hash;
if(!file_exists($file)) {
if(!self::resizeImage($original, $file, $xsize, $ysize, $xtype, $ytype)) {
throw new Exception("Resizing image failed");
}
}
return $file;
}
/*
public function remove($id) {
if ($id) {
$url = executeQuery('SELECT url FROM images WHERE id = :id', array(':id' => $id))->fetchOne();
if (substr(strtolower($url),0,4)=='http') {
return false;
} else {
unlink (self::getStorageLocation() .$url);
}
executeQuery('DELETE FROM images WHERE id = :id', array(':id' => $id));
}
}
*/
private static function getOriginalLocation() {
return dirname(__FILE__) . self::$originalLocation;
}
private static function getOriginalLocation2() {
return dirname(__FILE__) . self::$originalLocation2;
}
private static function getOriginalArtistLoc() {
return dirname(__FILE__) . self::$originalArtistLoc;
}
private static function getNewLocation() {
return dirname(__FILE__) . self::$newLocation;
}
private static function getHash($id, $xsize, $ysize, $xtype, $ytype) {
$hash = substr(md5($id . $xsize . $ysize . $xtype . $ytype . self::$seed), 0, 6);
return $hash;
}
private static function checkHash($id, $xsize, $ysize, $xtype, $ytype, $hash) {
return self::getHash($id, $xsize, $ysize, $xtype, $ytype) == $hash;
}
private static function resizeImage($src, $dst, $xsize, $ysize, $xtype, $ytype) {
switch(image_type_to_mime_type(exif_imagetype($src))) {
case "image/gif":
return self::resizeGif($src, $dst, $xsize, $ysize, $xtype, $ytype);
case "image/jpeg":
return self::resizeJpg($src, $dst, $xsize, $ysize, $xtype, $ytype);
case "image/png":
return self::resizePng($src, $dst, $xsize, $ysize, $xtype, $ytype);
}
return false;
}
private static function resizeJpg($src, $dst, $xsize, $ysize, $xtype, $ytype) {
$img = @imagecreatefromjpeg($src); //Surpress warning
if(!$img) {
return false;
}
list($width, $height) = @getimagesize($src);
$newsize = self::getNewSize($width, $height, $xsize, $ysize, $xtype, $ytype);
if ($newsize["height"] == $height && $newsize["width"] == $width) {
copy($src, $dst);
return true;
}
$img2 = imagecreatetruecolor($newsize['width'], $newsize['height']);
if($newsize['width'] / $newsize['height'] > $width / $height) {
$newWidth = $newsize['width'];
$newHeight = ceil($newsize['width'] / $width * $height);
$x = 0;
$y = floor(($newsize['height'] - $newHeight) / 2);
} else {
$newWidth = ceil($newsize['height'] / $height * $width);
$newHeight = $newsize['height'];
$x = floor(($newsize['width'] - $newWidth) / 2);
$y = 0;
}
imagecopyresampled($img2, $img, $x, $y, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($img2, $dst, self::$jpegQuality);
return true;
}
private static function resizePng($src, $dst, $xsize, $ysize, $xtype, $ytype) {
$img = imagecreatefrompng($src); //Surpress warning
if(!$img) {
return false;
}
list($width, $height) = @getimagesize($src);
$newsize = self::getNewSize($width, $height, $xsize, $ysize, $xtype, $ytype);
if ($newsize["height"] == $height && $newsize["width"] == $width) {
copy($src, $dst);
return true;
}
$img2 = imagecreatetruecolor($newsize['width'], $newsize['height']);
if($newsize['width'] / $newsize['height'] > $width / $height) {
$newWidth = $newsize['width'];
$newHeight = ceil($newsize['width'] / $width * $height);
$x = 0;
$y = floor(($newsize['height'] - $newHeight) / 2);
} else {
$newWidth = ceil($newsize['height'] / $height * $width);
$newHeight = $newsize['height'];
$x = floor(($newsize['width'] - $newWidth) / 2);
$y = 0;
}
imagecopyresampled($img2, $img, $x, $y, 0, 0, $newWidth, $newHeight, $width, $height);
imagepng($img2, $dst);
return true;
}
private function resizeGif($src, $dst, $xsize, $ysize, $xtype, $ytype) {
$img = imagecreatefromgif($src);
if(!$img) {
return false;
}
list($width, $height) = @getimagesize($src);
$newsize = Image::getNewSize($width, $height, $xsize, $ysize, $xtype, $ytype);
if ($newsize["height"] == $height && $newsize["width"] == $width) {
copy($src, $dst);
return true;
}
$img2 = imagecreatetruecolor($newsize['width'], $newsize['height']);
if($newsize['width'] / $newsize['height'] > $width / $height) {
$newWidth = $newsize['width'];
$newHeight = ceil($newsize['width'] / $width * $height);
$x = 0;
$y = floor(($newsize['height'] - $newHeight) / 2);
} else {
$newWidth = ceil($newsize['height'] / $height * $width);
$newHeight = $newsize['height'];
$x = floor(($newsize['width'] - $newWidth) / 2);
$y = 0;
}
$trnprt_indx = imagecolortransparent($img);
if ($trnprt_indx >= 0) {
imagetruecolortopalette($img2, true, 256);
imagealphablending($img2, false);
imagesavealpha($img2,true);
$trnprt_color = imagecolorsforindex($img, $trnprt_indx);
$trnprt_indx = imagecolorallocate($img2, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($img2, 0, 0, $trnprt_indx);
imagecolortransparent($img2, $trnprt_indx);
}
imagecopyresampled($img2, $img, $x, $y, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($img2, $dst);
return true;
}
private static function getNewSize($width, $height, $xsize, $ysize, $xtype, $ytype) {
$newsize = array("width" => $width, "height" => $height);
if($xtype == "exact" || ($xtype == "max" && $newsize['width'] > $xsize)) {
$newsize['height'] = round($newsize['height'] * $xsize / $newsize['width']);
$newsize['width'] = $xsize;
}
if($ytype == "exact" || ($ytype == "max" && $newsize['height'] > $ysize)) {
if($xtype != "exact") {
$newsize['width'] = round($newsize['width'] * $ysize / $newsize['height']);
if($xtype == "max") {
$newsize['width'] = min($newsize['width'], $xsize);
}
}
$newsize['height'] = $ysize;
}
return $newsize;
}
private static function getOriginal($id) {
// if ($id == 0) {
// return dirname(__FILE__) . '/../public_html/img2011/site/empty.gif';
// }
//$url = str_replace('aaserver01.autodisk.nl', 'AAFOTOS:#FOTOSAA0407@aaserver01.autodisk.nl', $url);
if ( substr($id,0,1) == 'a' ) {
return self::getArtistOriginal(substr($id,1));
}
$loc = self::getOriginalLocation2();
$file = $loc . $id . '.jpg';
if (!is_file($file)) {
$loc = self::getOriginalLocation();
$file = $loc . $id . '.jpg';
if (!is_file($file)) {
//throw new Exception('No original');
$c = @file_get_contents('http://artolive.nl/img/works/originals/' . $id . '.jpg');
if (!($c)) {
$c = @file_get_contents('http://artolive.nl/img/works/large/' . $id . '.jpg');
}
if (!$c) return $file;// throw new Exception("Cannot fetch original($file)");
file_put_contents($file, $c);
}
return $file;
}
return $file;
}
public function doesImageExists($id) {
return strlen($this->getOriginal($id)) > 1;
}
private static function getArtistOriginal($id) {
$loc = self::getOriginalArtistLoc();
$file = $loc . $id . '.jpg.original';
if (!is_file($file)) {
$loc = self::getOriginalArtistLoc();
$file = $loc . $id . '.jpg';
if (!is_file($file)) {
//throw new Exception('No original');
$c = @file_get_contents('http://artolive.nl/img/works/artists/' . $id . '.jpg.original');
if (!($c)) {
$c = @file_get_contents('http://artolive.nl/img/works/artists/' . $id . '.jpg');
}
if (!$c) return $file;// throw new Exception("Cannot fetch original($file)");
file_put_contents($file, $c);
}
return $file;
}
return $file;
}
}