Ответов: 6 Просмотров: 534
|
И так я долго искал способ уменьшить размер изображения не клонируя его и не сохраняя на сервере.
Наконец нашел решения.
И решил поделиться с вами, может кому пригодится.
Создайте файл thumbnail.php
С данным содержанием:
И положите его в корневую папку вашего сайта.
PHP:
скопировать код в буфер обмена
<?PHP function ErrorImage ($text) { global $maxw; if ($maxw < 154) $errw = 154; $errh = 30; $chrlen = intval (5 .9 * $len); $offset = intval (($errw - $chrlen) / 2 ); $im = imagecreate ($errw, $errh); /* Create a blank image */ header ("Content-type: image/jpeg"); } function thumbnail ($gdver, $src, $maxw=190) { for ($i=0; $i<count($gdarr); $i++) { if ($gdver != $gdarr[$i]) $test.="|"; } ErrorImage ("Incorrect GD version!"); } ErrorImage ("No image create functions!"); } if (!$size) { ErrorImage ("Image File Not Found!"); } else { if ($size[0] > $maxw) { $newy = intval ($size[1 ] * ($maxw / $size[0 ])); } else { $newx = $size[0]; $newy = $size[1]; } if ($gdver == 1) { } else { } if ($size[2] == 1) { ErrorImage ("Cannot Handle GIF Format!"); } else { if ($gdver == 1) imagecopyresized ($destimg, $sourceimg, 0 ,0 ,0 ,0 , $newx, $newy, $size[0 ], $size[1 ]); else @imagecopyresampled ($destimg, $sourceimg, 0 ,0 ,0 ,0 , $newx, $newy, $size[0 ], $size[1 ]) or die (ErrorImage ("Cannot use GD2 here!")); header ("content-type: image/gif"); } } elseif ($size[2]==2) { if ($gdver == 1) imagecopyresized ($destimg, $sourceimg, 0 ,0 ,0 ,0 , $newx, $newy, $size[0 ], $size[1 ]); else @imagecopyresampled ($destimg, $sourceimg, 0 ,0 ,0 ,0 , $newx, $newy, $size[0 ], $size[1 ]) or die (ErrorImage ("Cannot use GD2 here!")); header ("content-type: image/jpeg"); } elseif ($size[2] == 3) { if ($gdver == 1) imagecopyresized ($destimg, $sourceimg, 0 ,0 ,0 ,0 , $newx, $newy, $size[0 ], $size[1 ]); else @imagecopyresampled ($destimg, $sourceimg, 0 ,0 ,0 ,0 , $newx, $newy, $size[0 ], $size[1 ]) or die (ErrorImage ("Cannot use GD2 here!")); header ("content-type: image/png"); } else { ErrorImage ("Image Type Not Handled!"); } } } thumbnail ($_GET["gd"], $_GET["src"], $_GET["maxw"]); ?>
Теперь чтобы использовать его надо обращаться к этому файлу. Через $ _GET запрос. Т.е. так http://ВашСайт[dot]ru/thumbnail.php?gd=2&src=Имя папки/Название изображения.jpg&maxw=300
maxw = 300 это максимальное расширение изображения в px в которое оно скомпилюеться.
В результате получится нечто похожее на это http://bigtorrent-ua[dot]com/thumbna[dot][dot][dot]png&maxw=200
Оригинал: http://bigtorrent-ua[dot]com/bigtorrent-ua[dot]com[dot]png
Может кому пригодится |