/** * Сохраняет в нужном месте картинку анонса * @param $id * @param $fileData * @throws Exception */ public function saveImage($id, $fileData) { @mkdir($this->imagePath, 0777, true); if (!is_writeable($this->imagePath)) { throw new Exception('Directory is not writeable!'); } $img = $fileData instanceof AjaxFile ? $fileData->val() : $fileData; try { $rawImg = Images::data2image($img); $newImg = Images::scaleAndCrop($rawImg, $this->settings['width'], $this->settings['height'], 'png'); $bigImg = Images::scaleAndCrop($rawImg, $this->settings['bigWidth'], $this->settings['bigHeight'], 'png'); } catch (Exception $ex) { throw new Exception('Bad image format. ' . $ex->getMessage()); } try { file_put_contents($this->imagePath . '/' . $id . '.png', $newImg); file_put_contents($this->imagePath . '/' . $id . '-big.png', $bigImg); } catch (Exception $ex) { throw new Exception("Can't save image"); } }
/** * Добавление изображения * @param string|\Difra\Param\AjaxFile $image * @param bool $main * @throws \Difra\Exception */ public function addImage($image, $main = false) { $path = DIR_DATA . 'catalog/items/'; @mkdir($path, 0777, true); $this->save(); $this->load(); $useScaleAndCrop = \Difra\Config::getInstance()->getValue('catalog', 'usescale'); try { $rawImg = Images::data2image($image); } catch (\Difra\Exception $ex) { throw new \Difra\Exception('Bad image format.'); } $db = MySQL::getInstance(); $db->query('INSERT INTO `catalog_images` SET ' . "`item`='" . $db->escape($this->id) . "'," . "`main`=" . ($main ? '1' : '0')); $imgId = $db->getLastId(); foreach ($this->imgSizes as $k => $size) { if ($size) { if (is_null($useScaleAndCrop) || intval($useScaleAndCrop) == 0) { $newImg = Images::createThumbnail($rawImg, $size[0], $size[1], 'png'); } else { $newImg = Images::scaleAndCrop($rawImg, $size[0], $size[1], 'png'); } } else { $newImg = Images::convert($rawImg, 'png'); } if (file_put_contents($path . $imgId . $k . '.png', $newImg) === false) { throw new \Difra\Exception('Can\'t save image file.'); } } }