/** * Сохраняет в нужном месте картинку анонса * @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"); } }
public function updateValue($id, $value, $image = null) { $this->load(); $db = \Difra\MySQL::getInstance(); $db->query("UPDATE `catalog_ext_sets` SET `name`='" . $db->escape($value) . "' WHERE `id`='" . $db->escape($id) . "'"); if ($image and $this->set & self::SET_IMAGES) { $path = DIR_DATA . 'catalog/ext/'; @mkdir($path, 0777, true); $smallImage = \Difra\Libs\Images::scaleAndCrop($image, $this->imgWidth, $this->imgHeight, 'png', true); if (!$smallImage) { return self::BAD_IMAGE; } file_put_contents($path . $id . '.png', $smallImage); } return $id; }
/** * Добавление изображения * @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.'); } } }
/** * @param string|\Difra\Param\AjaxFile|\Difra\Param\AjaxFiles $image */ public function addImage($image) { if (!$image) { return; } elseif ($image instanceof \Difra\Param\AjaxFile) { $image = [$image->val()]; } elseif ($image instanceof \Difra\Param\AjaxFiles) { $image = $image->val(); } elseif (!is_array($image)) { $image = [$image]; } $path = DIR_DATA . 'gallery/'; @mkdir($path, 0777, true); $this->save(); $this->load(); $Config = \Difra\Config::getInstance(); $waterMarkOn = $Config->getValue('gallery', 'watermark'); $waterMarkOnPreview = $Config->getValue('gallery', 'waterOnPreview'); $waterText = $Config->getValue('gallery', 'waterText'); $watermarkImage = null; if (file_exists($path . 'watermark.png')) { $watermarkImage = file_get_contents($path . 'watermark.png'); } $db = \Difra\MySQL::getInstance(); $pos = intval($db->fetchOne("SELECT MAX(`position`) FROM `gallery_photos` WHERE `album`='" . $db->escape($this->id) . "'")) + 1; foreach ($image as $img) { $db->query('INSERT INTO `gallery_photos` SET ' . "`album`='" . $db->escape($this->id) . "'," . "`position`='{$pos}'"); $imgId = $db->getLastId(); foreach ($this->imgSizes as $k => $size) { if ($size) { $tmpImg = Images::scaleAndCrop($img, $size[0], $size[1], $this->format); if ($waterMarkOn && $waterMarkOnPreview) { if ($waterText != '') { $tmpImg = Images::setWatermark($tmpImg, $waterText, null, $this->format, 7); } elseif ($watermarkImage) { $tmpImg = Images::setWatermark($tmpImg, null, $watermarkImage, $this->format, 7); } } file_put_contents($path . $imgId . $k . '.' . $this->format, $tmpImg); } else { $tmpImg = Images::convert($img, $this->format); if ($waterMarkOn) { if ($waterText != '') { $tmpImg = Images::setWatermark($tmpImg, $waterText, null, $this->format, 15); } elseif ($watermarkImage) { $tmpImg = Images::setWatermark($tmpImg, null, $watermarkImage, $this->format, 15); } } file_put_contents($path . $imgId . $k . '.' . $this->format, $tmpImg); } } ++$pos; } }