1): unlink($fruta . $v); endif; endforeach; $sc = better_scandir($fruta2); foreach ($sc as $v): $s = filesize($fruta2 . $v); $s /= 1024; // Kb $s /= 1024; // Mb if ($s > 1): unlink($fruta2 . $v); endif; endforeach; */ $ext = pathinfo($file, PATHINFO_EXTENSION); $extoriginal = $ext; if (substr($file,0,4) == 'http'): // Si es externa, mira si hay una versión nueva en local (más nueva) if ($webp): $ext = 'webp'; $f2 = basename2($file); $p = strrpos($f2, '.'); $f2 = substr($f2, 0, $p) . '.webp'; $nfile = $isMobile . '_' . $size . '_' . md5($file) . '_' . $f2; else: $nfile = $isMobile . '_' . $size . '_' . md5($file) . '_old_' . basename2($file); endif; if (!file_exists($fruta2 . basename2($fileorg)) || ( file_exists($fruta. $nfile) && file_exists($fruta2 . basename2($fileorg)) && (filemtime($fruta. $nfile) < filemtime($fruta2 . basename2($fileorg))))): if (file_exists($fruta . $nfile)): unlink($fruta . $nfile); endif; if (file_exists($fruta2 . basename2($fileorg))): unlink($fruta2 . basename2($fileorg)); endif; endif; else: // Desactivamos webp $webp = false; $nfile = $isMobile . '_' . $size . '_' . md5($file) . '_' . basename2($file); endif; if (/*$_SERVER['REMOTE_ADDR'] != '46.27.140.85' && */file_exists($fruta . $nfile )): $s = file_get_contents($fruta . $nfile); if ($checksize): $img = imagecreatefromstring($s); $width = imagesx($img); $height = imagesy($img); echo $width . 'x' . $height; else: if (!isset($_GET['debug'])): if ($ext == 'png'): header('Content-Type: image/png'); elseif ($ext == 'webp'): header('Content-Type: image/webp'); else: header('Content-Type: image/jpeg'); endif; endif; echo $s; endif; exit; endif; if (!file_exists($fruta)): mkdir($fruta); else: $p = rand(0, 3000); if ($p == 25): // Limita la cantidad a n ficheros $sc=scandir($fruta); $c = count($sc); if ($c > $filelimit): $sc = better_scandir($fruta); $sc = array_slice($sc, 0, $c - $filelimit); // Borra carpeta foreach ($sc as $v): if (is_file($fruta . $v)): unlink($fruta . $v); endif; endforeach; endif; endif; endif; if (!file_exists($fruta2)): mkdir($fruta2); else: // Limita la cantidad a n ficheros $p = rand(0, 3000); if ($p == 25): $sc=scandir($fruta2); $c = count($sc); if ($c > $filelimit): $sc = better_scandir($fruta2); $sc = array_slice($sc, 0, $c - $filelimit); // Borra carpeta foreach ($sc as $v): if (is_file($fruta2 . $v)): unlink($fruta2 . $v); endif; endforeach; endif; endif; endif; include "../comun/bootstrap.php"; $f = $fileorg; $external = FALSE; if (substr($file,0,4) != 'http'): if (!empty($ruta) && strpos($f, $ruta) === 0): $f = substr($f, strlen($ruta)); endif; $file = eM_Gestor::cpath(str_replace('..','',$f)); // Comprueba si la ruta existe, si no existe, busca en imágenes comunes if (strpos($file, '/data/') !== FALSE): $b = strlen(basename2($file)); $rutatmp = str_replace('/core//','/',substr($file, 0, 0-$b)); $file = $rutatmp . basename2($file); endif; else: // Si es externa la copia en local si la fecha es más moderna $external = TRUE; $file = $fruta2 . basename2($f); // Copia condicional copydt($f, $file); $todelete = $file; //$file = $fruta2 . $nfile; endif; $aux = explode('_', $size); if (!empty($aux[1])): $p = explode('-', $aux[1]); if (!empty($p[1])): $aux[1] = $p[0]; $aux[2] = $p[1]; if (!$webp) $ext = $extoriginal; $forcewhite = TRUE; endif; endif; if (!file_exists($file) && strpos($file, '.webp') !== FALSE): $b = basename($file); $file = $fruta. '../../data/webp/' . $b; header('Content-Type: image/webp'); echo file_get_contents($file); exit; endif; if (!is_file($file)): header('HTTP/1.0 404 Not Found', true, 404); exit; endif; if ($size != 'x'): if (empty($aux[0]) || empty($aux[1])): list($ancho, $alto) = getimagesize($file); $ratio = $ancho/$alto; // width/height if (empty($aux[0])): $aux[0] = $aux[1] * $ratio; endif; if (empty($aux[1])): $aux[1] = $aux[0] / $ratio; endif; endif; if ($aux[0] > 2048): $prop = $aux[0] / $aux[1]; $aux[0] = 2048; $aux[1] = intval(2048 / $prop); elseif ($aux[1] > 2048): $prop = $aux[0] / $aux[1]; $aux[1] = 2048; $aux[0] = intval(2048 * $prop); endif; if ($aux[0] > 2048 || $aux[1] > 2048) die; $newwidth = $aux[0]; $newheight = $aux[1]; $fill = FALSE; if ($newwidth <= 640 && $isMobile): $globalquality = 80; else: $globalquality = 90; endif; $gray = false; if (!empty($aux[2])): if ($aux[2] == '1'): $fill = true; elseif ($aux[2] == '2'): $globalquality = 1; elseif ($aux[2] == '3'): $gray = true; elseif ($aux[2] == '4'): $gray = true; $fill = true; elseif ($aux[2] == '10'): $globalquality = 10; $fill = true; elseif ($aux[2] == 'x'): $globalquality = 1; $fill = true; endif; endif; else: // SIZE = X if ($webp): if ($isMobile): $globalquality = 60; else: $globalquality = 90; endif; convertToWebp($file, $fruta . $nfile); else: if ($ext == 'png'): header('Content-Type: image/png'); else: header('Content-Type: image/jpeg'); endif; echo file_get_contents($file); endif; exit; endif; if ($extoriginal == 'png' && !$external) $fill = true; $x = 0; $y = 0; if (!isset($_GET['debug']) && !$checksize): if ($ext == 'png'): header('Content-Type: image/png'); elseif ($ext == 'webp'): header('Content-Type: image/webp'); else: header('Content-Type: image/jpeg'); endif; endif; list($ancho, $alto) = getimagesize($file); $ratio = $ancho/$alto; // width/height if (!$fill): // Tipo de contenido if( $ratio <= 1) { $width = $newwidth; $height = $newwidth/$ratio; if ($height < $newheight) { $height = $newheight; $width = $height * $ratio; } } else { $width = $newheight*$ratio; $height = $newheight; if ($width < $newwidth) { $width = $newwidth; $height = $width / $ratio; } } // Cortamos la imagen $manipulator = new ImageManipulator($file); $manipulator->resample($width, $height); $width = $manipulator->getWidth(); $height = $manipulator->getHeight(); if ($checksize): echo $width . 'x' . $height; exit; endif; $centreX = round($width / 2); $centreY = round($height / 2); $x1 = $centreX - $newwidth/2; // 200 / 2 $y1 = $centreY - $newheight/2; // 130 / 2 $x2 = $centreX + $newwidth/2; // 200 / 2 $y2 = $centreY + $newheight/2; // 130 / 2 // center cropping to 200x130 //$newImage = $manipulator->crop($x1, $y1, $x2, $y2); if ($globalquality == 10): // Imagen blanca o transparente $im = imagecreatetruecolor($width, $height); if ($webp || $extoriginal == 'png'): imagesavealpha($im, true); $bg = imagecolorallocatealpha ( $im, 0, 0, 0, 127 ); imagefill($im,0,0,$bg); else: $bg = imagecolorallocate ( $im, 255, 255, 255 ); imagefilledrectangle($im,0,0,$width,$height,$bg); endif; else: $newImage = $manipulator->crop($x1, $y1, $x2, $y2); $im = $newImage->getResource(); imageinterlace($im, true); endif; if ($globalquality == 1): $bg = imagecolorallocate ( $im, 255, 255, 255 ); imagefilledrectangle($im,0,0,$width,$height,$bg); endif; if ($gray): imagecopymergegray($im, $im, 0, 0, 0, 0, $width, $height, 0); endif; if ($webp): // Mantiene transparencia imagewebp($im, null, $globalquality); imagewebp($im, $fruta. $nfile, $globalquality); else: imagejpeg($im, null, $globalquality); // Imprimir imagejpeg($im, $fruta. $nfile, $globalquality); endif; else: if ($checksize): echo intval($aux[0]) . 'x' . intval($aux[1]); exit; endif; if ($globalquality == 10 || $globalquality == 1): $im = imagecreatetruecolor($aux[0], $aux[1]); if ($webp || $extoriginal == 'png'): imagesavealpha($im, true); $bg = imagecolorallocatealpha ( $im, 0, 0, 0, 127 ); imagefill($im,0,0,$bg); else: /*$bg = imagecolorallocate ( $im, 255, 255, 255 ); imagefilledrectangle($im,0,0,$aux[0],$aux[1],$bg);*/ imagesavealpha($im, true); $bg = imagecolorallocatealpha ( $im, 0, 0, 0, 127 ); imagefill($im,0,0,$bg); $ext = 'png'; endif; $bg = imagecolorallocate ( $im, 200, 200, 200 ); $s = rand(0, $aux[0]-3); imagefilledrectangle($im, intval($s), intval($aux[1] - 3) ,intval($s+3), intval($aux[1]),$bg); else: $im = resize($file, $aux[0], $aux[1], $gray); endif; /*if ($globalquality == 1): $bg = imagecolorallocate ( $im, 255, 255, 255 ); imagefilledrectangle($im,0,0,$aux[0], $aux[1],$bg); endif;*/ imageinterlace($im, true); if ($ext == "jpg"): imagejpeg($im, null, $globalquality); imagejpeg($im, $fruta. $nfile, $globalquality); elseif ($ext == 'webp'): imagewebp($im, null, $globalquality); imagewebp($im, $fruta. $nfile, $globalquality); else: imagepng($im, $fruta . $nfile); imagepng($im, null); endif; endif; /*if ($external): unlink($todelete); endif;*/ function resize($source_image, $tn_w, $tn_h, $gray, $quality = 90, $wmsource = false) { $info = getimagesize($source_image); $imgtype = image_type_to_mime_type($info[2]); #assuming the mime type is correct switch ($imgtype) { case 'image/jpeg': $source = imagecreatefromjpeg($source_image); break; case 'image/gif': $source = imagecreatefromgif($source_image); break; case 'image/png': $source = imagecreatefrompng($source_image); break; default: die('Invalid image type.'); } #Figure out the dimensions of the image and the dimensions of the desired thumbnail $src_w = imagesx($source); $src_h = imagesy($source); #Do some math to figure out which way we'll need to crop the image #to get it proportional to the new size, then crop or adjust as needed $x_ratio = $tn_w / $src_w; $y_ratio = $tn_h / $src_h; if (($src_w <= $tn_w) && ($src_h <= $tn_h)) { $new_w = $src_w; $new_h = $src_h; } elseif (($x_ratio * $src_h) < $tn_h) { $new_h = ceil($x_ratio * $src_h); $new_w = $tn_w; } else { $new_w = ceil($y_ratio * $src_w); $new_h = $tn_h; } $newpic = imagecreatetruecolor(round($new_w), round($new_h)); if ($imgtype != 'image/jpeg'): imagealphablending( $newpic, false ); imagesavealpha( $newpic, true ); endif; imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h); if ($gray): imagecopymergegray($newpic, $newpic, 0, 0, 0, 0, $new_w, $new_h, 0); endif; $final = imagecreatetruecolor($tn_w, $tn_h); if ($imgtype == 'image/jpeg'): $backgroundColor = imagecolorallocate($final, 255, 255, 255); else: imagesavealpha($final, true); $backgroundColor = imagecolorallocatealpha($final, 0, 0, 0, 127); endif; imagefill($final, 0, 0, $backgroundColor); //imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h); imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h); return $final; } class ImageManipulator { /** * @var int */ protected $width; /** * @var int */ protected $height; /** * @var resource */ protected $image; /** * Image manipulator constructor * * @param string $file OPTIONAL Path to image file or image data as string * @return void */ public function __construct($file) { if (!empty($file)) { if (is_file($file)) { $this->setImageFile($file); } else { $this->setImageString($file); } } } /** * Set image resource from file * * @param string $file Path to image file * @return ImageManipulator for a fluent interface * @throws InvalidArgumentException */ public function setImageFile($file) { if (!(is_readable($file) && is_file($file))) { throw new InvalidArgumentException("Image file $file is not readable"); } if (is_resource($this->image)) { imagedestroy($this->image); } list ($this->width, $this->height, $type) = getimagesize($file); switch ($type) { case IMAGETYPE_GIF : $this->image = imagecreatefromgif($file); break; case IMAGETYPE_JPEG : $this->image = imagecreatefromjpeg($file); break; case IMAGETYPE_PNG : $this->image = imagecreatefrompng($file); break; case IMAGETYPE_WEBP : $this->image = imagecreatefromwebp($file); break; default : throw new InvalidArgumentException("Image type $type not supported"); } return null; } /** * Set image resource from string data * * @param string $data * @return ImageManipulator for a fluent interface * @throws RuntimeException */ public function setImageString($data) { if (is_resource($this->image)) { imagedestroy($this->image); } if (!$this->image = imagecreatefromstring($data)) { throw new RuntimeException('Cannot create image from data string'); } $this->width = imagesx($this->image); $this->height = imagesy($this->image); return $this; } /** * Resamples the current image * * @param int $width New width * @param int $height New height * @param bool $constrainProportions Constrain current image proportions when resizing * @return ImageManipulator for a fluent interface * @throws RuntimeException */ public function resample($width, $height, $constrainProportions = true) { /*if (!is_resource($this->image)) { throw new RuntimeException('No image set'); }*/ if ($constrainProportions) { if ($this->height >= $this->width) { $width = round($height / $this->height * $this->width); } else { $height = round($width / $this->width * $this->height); } } $temp = imagecreatetruecolor($width, $height); imagecopyresampled($temp, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height); return $this->_replace($temp); } /** * Enlarge canvas * * @param int $width Canvas width * @param int $height Canvas height * @param array $rgb RGB colour values * @param int $xpos X-Position of image in new canvas, null for centre * @param int $ypos Y-Position of image in new canvas, null for centre * @return ImageManipulator for a fluent interface * @throws RuntimeException */ public function enlargeCanvas($width, $height, array $rgb = array(), $xpos = null, $ypos = null) { if (!is_resource($this->image)) { throw new RuntimeException('No image set'); } $width = max($width, $this->width); $height = max($height, $this->height); $temp = imagecreatetruecolor($width, $height); if (count($rgb) == 3) { $bg = imagecolorallocate($temp, $rgb[0], $rgb[1], $rgb[2]); imagefill($temp, 0, 0, $bg); } if (null === $xpos) { $xpos = round(($width - $this->width) / 2); } if (null === $ypos) { $ypos = round(($height - $this->height) / 2); } imagecopy($temp, $this->image, (int) $xpos, (int) $ypos, 0, 0, $this->width, $this->height); return $this->_replace($temp); } /** * Crop image * * @param int|array $x1 Top left x-coordinate of crop box or array of coordinates * @param int $y1 Top left y-coordinate of crop box * @param int $x2 Bottom right x-coordinate of crop box * @param int $y2 Bottom right y-coordinate of crop box * @return ImageManipulator for a fluent interface * @throws RuntimeException */ public function crop($x1, $y1 = 0, $x2 = 0, $y2 = 0) { /*if (!is_resource($this->image)) { throw new RuntimeException('No image set'); }*/ if (is_array($x1) && 4 == count($x1)) { list($x1, $y1, $x2, $y2) = $x1; } $x1 = intval(max($x1, 0)); $y1 = intval(max($y1, 0)); $x2 = intval(min($x2, $this->width)); $y2 = intval(min($y2, $this->height)); $width = intval($x2 - $x1); $height = intval($y2 - $y1); $temp = imagecreatetruecolor($width, $height); imagecopy($temp, $this->image, 0, 0, $x1, $y1, $width, $height); return $this->_replace($temp); } /** * Replace current image resource with a new one * * @param resource $res New image resource * @return ImageManipulator for a fluent interface * @throws UnexpectedValueException */ protected function _replace($res) { /* if (!is_resource($res)) { throw new UnexpectedValueException('Invalid resource'); }*/ if (is_resource($this->image)) { imagedestroy($this->image); } $this->image = $res; $this->width = imagesx($res); $this->height = imagesy($res); return $this; } /** * Save current image to file * * @param string $fileName * @return void * @throws RuntimeException */ public function save($fileName, $type = IMAGETYPE_JPEG) { $dir = dirname($fileName); if (!is_dir($dir)) { if (!mkdir($dir, 0755, true)) { throw new RuntimeException('Error creating directory ' . $dir); } } try { switch ($type) { case IMAGETYPE_GIF : if (!imagegif($this->image, $fileName)) { throw new RuntimeException; } break; case IMAGETYPE_PNG : if (!imagepng($this->image, $fileName)) { throw new RuntimeException; } break; case IMAGETYPE_JPEG : default : if (!imagejpeg($this->image, $fileName, 95)) { throw new RuntimeException; } } } catch (Exception $ex) { throw new RuntimeException('Error saving image file to ' . $fileName); } } /** * Returns the GD image resource * * @return resource */ public function getResource() { return $this->image; } /** * Get current image resource width * * @return int */ public function getWidth() { return $this->width; } /** * Get current image height * * @return int */ public function getHeight() { return $this->height; } } function basename2($file) { $file = basename($file); $p = strpos($file, '?'); if ($p !== FALSE): $file = substr($file, 0, $p); endif; return $file; } function convertToWebp($file, $salida) { global $globalquality; $info = getimagesize($file); $imgtype = image_type_to_mime_type($info[2]); #assuming the mime type is correct switch ($imgtype) { case 'image/jpeg': $pngimg = imagecreatefromjpeg($file); break; case 'image/gif': $pngimg = imagecreatefromgif($file); break; case 'image/png': $pngimg = imagecreatefrompng($file); break; default: die('Invalid image type.'); } // get dimens of image $w = imagesx($pngimg); $h = imagesy($pngimg);; // create a canvas $im = imagecreatetruecolor ($w, $h); if ($imgtype != 'image/jpeg'): imageAlphaBlending($im, false); imageSaveAlpha($im, true); // By default, the canvas is black, so make it transparent $trans = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefilledrectangle($im, 0, 0, $w - 1, $h - 1, $trans); endif; // copy png to canvas imagecopy($im, $pngimg, 0, 0, 0, 0, $w, $h); // lastly, save canvas as a webp header('Content-Type: image/webp'); imagewebp($im, null, $globalquality); imagewebp($im, $salida, $globalquality); // done imagedestroy($im); } function better_scandir($dir, $sorting_order = SCANDIR_SORT_ASCENDING) { /****************************************************************************/ // Roll through the scandir values. $files = array(); foreach (scandir($dir, $sorting_order) as $file) { if ($file[0] === '.') { continue; } $files[$file] = filemtime($dir . '/' . $file); } // foreach /****************************************************************************/ // Sort the files array. if ($sorting_order == SCANDIR_SORT_ASCENDING) { asort($files, SORT_NUMERIC); } else { arsort($files, SORT_NUMERIC); } /****************************************************************************/ // Set the final return value. $ret = array_keys($files); /****************************************************************************/ // Return the final value. return ($ret) ? $ret : false; } // better_scandir function copydt($pathSource, $pathDest) { if (!file_exists($pathDest)) { copy($pathSource, $pathDest); } } function copydt_alternative($pathSource, $pathDest) { // copy(), same modification-time $curl = curl_init($pathSource); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FILETIME, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); $result = curl_exec($curl); if ($result === false): die (curl_error($curl)); endif; $down = false; $timestamp = curl_getinfo($curl, CURLINFO_FILETIME); if (!file_exists($pathDest) || $timestamp > filemtime($pathDest)) : $down = true; endif; if ($down): copy($pathSource, $pathDest); touch($pathDest, $timestamp); endif; } ?>