proWoke
Новичок
Здравствуйте, я временно забивал на PHP программирование и учил javascript. Вот теперь пишу интернет магазин. И всё хочу делать с классами и объектами. Но т.к. книгу по Теории программирования на ООП я буду читать лишь после нового года, до этого хочу много кодить, а то я только читаю статьи и книги, а программировать толком не программирую. Так вот пока я пытаюсь хоть что-то похожее на ООП писать. Так вот написал такой вот класс:
Он принимает картинку и режет её как мне надо и сохраняет в указанный путь.
Вот как его использовать:
Вот сам класс:
Вот файл album_db.php, который добавляет данные в базу.
Он принимает картинку и режет её как мне надо и сохраняет в указанный путь.
Вот как его использовать:
PHP:
if (@$_REQUEST['send']) {
$path_to_mini_dir = "/pdo/images/mini/";
$path_to_big_dir = "/pdo/images/big/";
$albumimg = new Form_Img($_FILES['img']);
$albumimg->proof();
$albumimg->setMini(100, 100, $server.$path_to_mini_dir);
$albumimg->setBig(500, 500, $server.$path_to_big_dir);
$albumimg->resize();
$albumimg->save();
$fileName = $albumimg->getFilename();
$name = $_REQUEST['name'];
$musician = $_REQUEST['musician'];
$path_to_mini_img = $path_to_mini_dir.$fileName;
$path_to_big_img = $path_to_big_dir.$fileName;
$CDtype = $_REQUEST['CDtype'];
$year = $_REQUEST['year'];
include($server."/pdo/admin/new/album_db.php");
PHP:
<?php
class Form_Img {
protected $time;
protected $infoarray;
protected $exist;
protected $ext;
protected $mime;
protected $fileName;
protected $currentheight;
protected $currentwidth;
protected $bigheight;
protected $bigwidth;
protected $miniheight;
protected $miniwidth;
protected $minipath;
protected $bigpath;
protected $mini;
protected $big;
public function __construct($infoarray) {
$this->infoarray = $infoarray;
$this->time = time();
}
public function proof() {
if (file_exists($this->infoarray['tmp_name']))
$this->exist = true;
if ($this->exist) {
if (preg_match('{image\/(.*)}is',$this->infoarray['type'], $this->ext))
$this->fileName = $this->time.".".$this->ext[1];
}
}
public function setMini($height, $width, $path) {
$this->miniwidth = $width;
$this->miniheight = $height;
$this->minipath = $path.$this->fileName;
}
public function setBig($height, $width, $path) {
$this->bigwidth = $width;
$this->bigheight = $height;
$this->bigpath = $path.$this->fileName;
}
public function resize() {
$this->mini = new Imagick($this->infoarray['tmp_name']);
$this->big = new Imagick($this->infoarray['tmp_name']);
$this->currentheight = $this->mini->getImageHeight();
$this->currentwidth = $this->mini->getImageWidth();
if ($this->currentheight < $this->currentwidth) {
$this->mini->resizeImage($this->miniwidth,$this->miniheight, Imagick::FILTER_LANCZOS,1);
$this->big->resizeImage($this->bigwidth,$this->bigheight,Imagick::FILTER_LANCZOS,1);
} else {
$this->mini->resizeImage($this->miniheight,$this->miniwidth,Imagick::FILTER_LANCZOS,1);
$this->big->resizeImage($this->bigheight,$this->bigwidth,Imagick::FILTER_LANCZOS,1);
}
}
public function save() {
$this->mini->writeImage($this->minipath);
$this->big->writeImage($this->bigpath);
}
public function __destruct() {
if (isset($this->mini))
$this->mini->destroy();
if (isset($this->big))
$this->big->destroy();
}
public function getFilename() {
return $this->fileName;
}
}
PHP:
$dataobj = array($name, $musician, $path_to_mini_img, $path_to_big_img, $CDtype, $year);
try {
$query = $DBID->prepare("INSERT INTO album (name, musician, miniimg, bigimg, CDtype, year) value (?, ?, ?, ?, ?, ?)");
$query->execute($dataobj);
} catch(PDOException $e) {
echo $e->getMessage();
}