Şi mi-e atât de dor de visul nostru,
De trupul tău uitat sub palma mea,
De nopţile senine şi curate
Cand m-alintai cu-n gând rebel pe-o stea...
31 mai 2006
<?php
/**
* Parametrii la care va fi redimensionata imaginea
**/
$output_width = (isset($_REQUEST['w']) && is_numeric($_REQUEST['w'])) ? $_REQUEST['w'] : 150;
$output_height = (isset($_REQUEST['h']) && is_numeric($_REQUEST['h'])) ? $_REQUEST['h'] : 150;
/**
* Imaginea originala si array-ul cu proprietatile imaginii
**/
$path = ((isset($_REQUEST['path']))? $_REQUEST['path'] : "");
$size_arr = getimagesize($path);
if ($size_arr[2]==IMAGETYPE_GIF) $image = imagecreatefromgif($path);
else if ($size_arr[2]==IMAGETYPE_JPEG) $image = imagecreatefromjpeg($path);
else if ($size_arr[2]==IMAGETYPE_PNG) $image = imagecreatefrompng($path);
else die_default_image();
$tmp = tempnam(sys_get_temp_dir(), "phptmp");
resize_image($tmp, $image, $size_arr, $output_width, $output_height);
/**
* Setarea header-ului si returnarea continutului
**/
header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename="'.basename($path).'"');
echo file_get_contents($tmpname); unlink($tmp); die('');
/**
* IMAGINE DEFAULT - In caz de eroare, gif transparent de 1x1 pixeli
**/
function die_default_image() {
header("content-type: image/gif");
echo base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
}
/**
* Functia de redimensionare a imaginii
* Format JPEG, calitate 80%
**/
function resize_image($thumbname, $image, $size_arr, $max_width, $max_height) {
// Mentinerea aspectului
$width = $max_width;
$height = $max_height;
list($width_orig, $height_orig, $img_type) = $size_arr;
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) $width = floor($height*$ratio_orig);
else $height = floor($width/$ratio_orig);
// Redimensionarea
$tempimg = imagecreatetruecolor($width, $height);
imagecopyresampled($tempimg, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($tempimg, $thumbname, 80);
}
/**
* Pentru versiuni PHP mai vech, definire director temporar
**/
if (!function_exists('sys_get_temp_dir')) {
function sys_get_temp_dir() {
if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
if (!empty($_ENV['TMPDIR'])) { return realpath($_ENV['TMPDIR']); }
if (!empty($_ENV['TEMP'])) { return realpath($_ENV['TEMP']); }
$tempfile = tempnam(uniqid(rand(),TRUE),'');
if (file_exists($tempfile)) {
unlink($tempfile);
return realpath(dirname($tempfile));
}
}
}
?>
http://myDomain.tld/minipic.php?h=100&w=100&path=http://test.images/image.png
<img src="http://myDomain.tld/minipic.php?h=100&w=100&path=http://test.images/image.png" alt="imaginea_mea" />
