/** * Upload the image */ public function actionUploadImage() { Yii::$app->response->format = Response::FORMAT_JSON; if (!empty($_FILES['file']['tmp_name'])) { if ($image = Image::load($_FILES['file']['tmp_name'], $this->_module->imageDriver)) { if (!empty($this->_module->maxImageResolution)) { $resolution = explode('x', strtolower($this->_module->maxImageResolution)); $width = empty($resolution[0]) ? null : $resolution[0]; $height = empty($resolution[1]) ? null : $resolution[1]; if ($image->width > $width && $image->height > $height) { $image->resize($width, $height, 'auto'); } } $extension = strtolower(image_type_to_extension($image->type, true)); if (!in_array($extension, ['.jpg', '.gif', '.png'])) { $extension = '.jpg'; } $filename = $this->uniqueRandomFilename($this->_module->imageUploadPath, $extension); if ($image->save("{$this->_module->imageUploadPath}/{$filename}{$extension}")) { return ['filelink' => "{$this->_module->imageBaseUrl}/{$filename}{$extension}"]; } } } return []; }
/** * Upload the images. * @param Event $event * @throws ErrorException */ public function uploadImages($event) { if (!empty($this->attributes)) { $modelName = array_reverse(explode('\\', $this->owner->className()))[0]; if (!empty($_FILES[$modelName]['tmp_name'])) { $save = false; $files =& $_FILES[$modelName]['tmp_name']; if (!$this->owner->hasMethod('getPrimaryKey', false)) { throw new ErrorException(sprintf('Cannot get primary key of class %s', $this->owner->className())); } /** @var int $primaryKey */ $primaryKey = call_user_func([$this->owner, 'getPrimaryKey']); $upload_dir = Yii::getAlias($this->upload_dir, true); foreach ($this->attributes as $attribute => $settings) { if (!empty($files[$attribute])) { $save = true; $settings = array_merge($this->defaultImagesSettings, $settings); $filename = $primaryKey . '-' . Yii::$app->security->generateRandomString(mt_rand(5, 12)); if (!in_array($attribute, $this->owner->attributes())) { throw new ErrorException(sprintf('Cannot get attribute %s of class %s', $attribute, $this->owner->className())); } foreach ($settings['sizes'] as $name => $options) { if (is_array($options)) { $options = array_merge($settings, $options); } else { $options = array_merge($settings, ['size' => $options]); } if ($image = Image::load($files[$attribute], $this->imageDriver)) { $path = $this->schemaTo($upload_dir, $attribute, $name); if (!empty($this->owner->{$attribute})) { @unlink("{$path}/{$this->owner->{$attribute}}.{$options['format']}"); } if ($options['size'] !== 'original') { $info = explode('x', strtolower($options['size'])); $width = empty($info[0]) ? null : $info[0]; $height = empty($info[1]) ? null : $info[1]; $image->resize($width, $height, $options['master'])->background($options['background']); } if ($image->save("{$path}/{$filename}.{$options['format']}", $options['quality'])) { $this->owner->{$attribute} = $filename; } else { $this->owner->{$attribute} = ''; } } } unset($files[$attribute]); } } if ($save && $event->name === BaseActiveRecord::EVENT_AFTER_UPDATE) { $this->owner->save(false); } } } }
/** * Watermark. * @param Image $image * @param integer $offset_x * @param integer $offset_y * @param integer $opacity */ protected function _watermark(Image $image, $offset_x, $offset_y, $opacity) { $watermark = new \Imagick(); $watermark->readImageBlob($image->render(), $image->filename); if ($watermark->getImageAlphaChannel() !== \Imagick::ALPHACHANNEL_ACTIVATE) { $watermark->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE); } if ($opacity < 100) { $watermark->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $opacity / 100, \Imagick::CHANNEL_ALPHA); } $this->im->compositeImage($watermark, \Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y); }
/** * Watermark. * @param Image $watermark * @param integer $offset_x * @param integer $offset_y * @param integer $opacity */ protected function _watermark(Image $watermark, $offset_x, $offset_y, $opacity) { $overlay = imagecreatefromstring($watermark->render()); imagesavealpha($overlay, true); $width = imagesx($overlay); $height = imagesy($overlay); if ($opacity < 100) { $opacity = round(abs($opacity * 127 / 100 - 127)); $color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity); imagelayereffect($overlay, IMG_EFFECT_OVERLAY); imagefilledrectangle($overlay, 0, 0, $width, $height, $color); } imagealphablending($this->image, true); imagecopy($this->image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height); imagedestroy($overlay); }