Skip to content

Instantly share code, notes, and snippets.

@diogomachado
Created July 18, 2017 01:12
Show Gist options
  • Save diogomachado/e0caf3b11a7e7fa6c550e9726a3dc7ce to your computer and use it in GitHub Desktop.
Save diogomachado/e0caf3b11a7e7fa6c550e9726a3dc7ce to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function createImage($filename){
$pathinfo = pathinfo($filename);
$info = getimagesize($filename);
$extension_arr = explode('?', $pathinfo['extension']);
$extension = $extension_arr[0];
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext = 'jpg';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
default:
throw new Exception("Formato não aceito ${mime}");
break;
}
$image_save_func($image_create_func($filename), "store/".sha1($filename).".${extension}");
return $info;
}
function connectDatabase(){
try{
$link = new \PDO( 'mysql:host=127.0.0.1;dbname=imagem;',
'imagem_user',
'<PASSWORD>',
array(
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_PERSISTENT => false
)
);
return $link;
}
catch(\PDOException $ex){
print($ex->getMessage());
}
}
function responseAPI($code, $message){
http_response_code($code);
echo json_encode(array('status'=> $code, 'message'=> $message));
die();
}
if (!isset($_GET['url'])){
responseAPI(500, 'Parâmetro url não informado');
}
if (!isset($_GET['width'])){
responseAPI(500, 'Parâmetro width não informado');
}
// GET parameters
$url = $_GET['url'];
$width_fetch = $_GET['width'];
// Key unique
$chave = sha1($url.$width_fetch);
$link = connectDatabase();
$handle = $link->prepare('SELECT arq.* FROM arquivos arq WHERE chave = ?');
$handle->bindValue(1, $chave);
$handle->execute();
$result = $handle->fetchAll(\PDO::FETCH_OBJ);
if ($result) {
$loaded = $result[0];
$cachefile = 'cache/'.$chave.'.'.$loaded->extensao;
$info = getimagesize($cachefile);
}
else {
$info = createImage($_GET['url']);
$chave_file = sha1($url);
$arr_pesquisa = glob("store/${chave_file}.*");
if (count($arr_pesquisa) > 0){
$filename = $arr_pesquisa[0];
}else{
responseAPI(500, "Arquivo (${chave_file}) não encontrado em STORE.");
}
// Original sizes (width, height)
$width_orig = $info[0];
$height_orig = $info[1];
// Proportional height
$width = $width_fetch;
$height = round((($height_orig/$width_orig) * $width));
$extensao_arr = explode('/', $info['mime']);
$extensao = $extensao_arr[1];
$filename = 'store/'.$chave_file.'.'.$extensao;
$cachefile = 'cache/'.$chave.'.'.$extensao;
if ($extensao == "jpg" || $extensao == "jpeg"){
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $cachefile, 75);
}
else if ($extensao == "png"){
$image_p = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($image_p, 0, 0, 0);
imagecolortransparent($image_p, $background);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagepng($image_p, $cachefile, 5);
}
$handle = $link->prepare('INSERT INTO arquivos(id, width, height, url, extensao, chave) VALUES(NULL, ?, ?, ?, ?, ?);');
$handle->bindValue(1, $width_fetch, PDO::PARAM_INT);
$handle->bindValue(2, $height, PDO::PARAM_INT);
$handle->bindValue(3, urlencode($url));
$handle->bindValue(4, $extensao);
$handle->bindValue(5, $chave);
$handle->execute();
}
// Close connection
$link = null;
// Send header
header('Content-Type: '.$info['mime']);
// Show file
readfile($cachefile);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment