Автор Тема: Клас за обработка на изображения  (Прочетена 4855 пъти)

0 Потребители и 1 Гост преглежда(т) тази тема.

HanKrum

  • Hero Member
  • *****
  • Благодарности
  • -Казани: 125
  • -Получени: 73
  • Публикации: 1005
  • Кибик
Клас за обработка на изображения
« -: 20 Октомври 2012, 16:22:14 »
Тук има и пример за използване на променливи променливи.
Код: PHP
  1. <?php
  2.  
  3. /**
  4.  * Description of PicSet
  5.  *
  6.  * Работи със jpeg, gif и png
  7.  * Клас за проверка на една снимка, дали е потретна, пейзажна или квадрат.
  8.  * Връща данни за снимката като директория, име, тип, височина и ширина
  9.  * Също така и преоразмерява снимката като запазва съотношението и/или създава черно-бяло копие.
  10.  *
  11.  * @author hankrum
  12.  */
  13. class PicSet {
  14.  
  15.     private $destination; //Път до изображението
  16.     private $size; //Максимална височина или широчина на изображението
  17.     private $type; //Типа снимка
  18.     private $post_width;
  19.     private $post_height;
  20.  
  21.     public function __construct($destination) {
  22.         $this->destination = $destination;
  23.         $mime = mime_content_type($this->destination);
  24.         if (preg_match('/image/', $mime)) {
  25.             if (preg_match('/jpeg$|png$|gif$/', $mime)) {
  26.                 $this->type = str_replace('image/', '', $mime);
  27.             } else {
  28.                 echo 'Неподдържан формат, само jpeg(jpeg), gif и png!!!';
  29.                 exit();
  30.             }
  31.         } else {
  32.             echo 'Това не е изображение!!!';
  33.             exit();
  34.         }
  35.     }
  36.  
  37.     public function get_type() {
  38.         return $this->type;
  39.     }
  40.  
  41.     public function get_dir() {
  42.         return $this->setDIR();
  43.     }
  44.  
  45.     public function get_name() {
  46.         return $this->setNAME();
  47.     }
  48.  
  49.     public function get_height() {
  50.         return $this->setY();
  51.     }
  52.  
  53.     public function get_width() {
  54.         return $this->setX();
  55.     }
  56.  
  57.     public function intIMG_profil() {
  58.         if ($this->setX() > $this->setY()) {//Ако снимката е пейзажна
  59.             return 2;
  60.         } elseif ($this->setY() > $this->setX()) {//Ако снимката е портретна
  61.             return 1;
  62.         } elseif ($this->setY() == $this->setX()) {//Ако снимката е квадрат
  63.             return 0;
  64.         }
  65.     }
  66.  
  67.     public function resizeIMG($size = 100, $bw = false) {
  68.         $this->size = $size;
  69.         $method = 'resize_' . $this->type;
  70.         return $this->$method($bw);
  71.     }
  72.  
  73.     private function setDIR() {
  74.         $dir = dirname($this->destination); //Дава директорията
  75.         return $dir;
  76.     }
  77.  
  78.     private function setNAME() {
  79.         $name = basename($this->destination); //Дава името на изображението
  80.         return $name;
  81.     }
  82.  
  83.     private function setX() { //Определя широчината
  84.         $width = imagesx($this->setIMG());
  85.         return $width;
  86.     }
  87.  
  88.     private function setY() { //Определя височината
  89.         $height = imagesy($this->setIMG());
  90.         return $height;
  91.     }
  92.  
  93.     private function reIMG() { //Задава съотношението и оразмерява
  94.         if ($this->intIMG_profil() == 2) {//Ако снимката е пейзажна
  95.             $this->post_height = floor($this->setY() * ($this->size / $this->setX()));
  96.             $this->post_width = $this->size;
  97.         } elseif ($this->intIMG_profil() == 1) {//Ако снимката е портретна
  98.             $this->post_width = floor($this->setX() * ($this->size / $this->setY()));
  99.             $this->post_height = $this->size;
  100.         } elseif ($this->intIMG_profil() == 0) {//Ако снимката е квадрат
  101.             $this->post_height = $this->size;
  102.             $this->post_width = $this->size;
  103.         }
  104.     }
  105.  
  106.     private function setIMG() { //Създава копие със същите размери
  107.         $method = 'imagecreatefrom' . $this->type;
  108.         $img = $method($this->destination);
  109.         return $img;
  110.     }
  111.  
  112.     private function resize_jpeg($bw = false) {
  113.         $this->reIMG();
  114.         //Оразмеряване на изображението
  115.         $tmpp_img = imagecreatetruecolor($this->post_width, $this->post_height);
  116.         imagecopyresized($tmpp_img, $this->setIMG(), 0, 0, 0, 0, $this->post_width, $this->post_height, $this->setX(), $this->setY());
  117.         if ($bw === true) { // Създава чернобяло копие
  118.             imagefilter($tmpp_img, IMG_FILTER_GRAYSCALE);
  119.         }
  120.         imagejpeg($tmpp_img, $this->setDIR() . DIRECTORY_SEPARATOR . $this->setNAME());
  121.     }
  122.  
  123.     private function resize_png($bw = false) {
  124.         $this->resize_gif_png();
  125.     }
  126.  
  127.     private function resize_gif($bw = false) {
  128.         $this->resize_gif_png();
  129.     }
  130.  
  131.     private function resize_gif_png() {
  132.         $this->reIMG();
  133.         //Оразмеряване на изображението
  134.         $tmpp_img = imagecreatetruecolor($this->post_width, $this->post_height);
  135.         imagealphablending($tmpp_img, false);
  136.         imagesavealpha($tmpp_img, true);
  137.         $transparent = imagecolorallocatealpha($tmpp_img, 255, 255, 255, 127);
  138.         imagefilledrectangle($tmpp_img, 0, 0, $this->post_width, $this->post_height, $transparent);
  139.         imagecopyresized($tmpp_img, $this->setIMG(), 0, 0, 0, 0, $this->post_width, $this->post_height, $this->setX(), $this->setY());
  140.         if ($bw === true) {
  141.             imagefilter($tmpp_img, IMG_FILTER_GRAYSCALE);
  142.         }
  143.         $function = 'image' . $this->type;
  144.         $function($tmpp_img, $this->setDIR() . DIRECTORY_SEPARATOR . $this->setNAME());
  145.     }
  146.  
  147. }

Пример:
Код: PHP
  1. <?php
  2.  
  3. echo '<form method="POST" enctype="multipart/form-data">
  4.        Picture:<input type="file" name="pic" /><input type="submit" /><br />
  5.      </form>';
  6. include_once 'PicSet.php';
  7. if ($_FILES['pic']['tmp_name']) {
  8.     move_uploaded_file($_FILES['pic']['tmp_name'], 'pic' . DIRECTORY_SEPARATOR . $_FILES['pic']['name']);
  9.     $pic = new PicSet('pic' . DIRECTORY_SEPARATOR . $_FILES['pic']['name']);
  10.     echo 'Type is:' . $pic->get_type() . '<br />';
  11.     echo 'Name is:' . $pic->get_name() . '<br />';
  12.     echo 'Dir is:' . $pic->get_dir() . '<br />';
  13.     echo 'Height is:' . $pic->get_height() . '<br />';
  14.     echo 'Width is:' . $pic->get_width() . '<br />';
  15.     echo 'Profil is:' . $pic->intIMG_profil() . '<hr />';
  16.     $pic->resizeIMG();
  17.     $pic = new PicSet('pic' . DIRECTORY_SEPARATOR . $_FILES['pic']['name']);
  18.     echo 'Type is:' . $pic->get_type() . '<br />';
  19.     echo 'Name is:' . $pic->get_name() . '<br />';
  20.     echo 'Dir is:' . $pic->get_dir() . '<br />';
  21.     echo 'Height is:' . $pic->get_height() . '<br />';
  22.     echo 'Width is:' . $pic->get_width() . '<br />';
  23.     echo 'Profil is:' . $pic->intIMG_profil() . '<hr />';
  24. }
« Последна редакция: 20 Октомври 2012, 16:23:48 от HanKrum »
"Силата на правителството се крепи на невежеството на народа, и те знаят това и винаги ще се борят против просвещението." Лев Толстой