I am using yii\imagine\Image extension and want to add a watermark to my image.
Here's my code:
$watermarkImage = '#webroot/../images/watermark.png';
$image = '#webroot/../slike/img-4.jpg';
Image::watermark($image, $watermarkImage);
After this code is executed, nothing happens. What am I missing here?
The ::watermark() function creates the new image but doesn't automatically saves it. The function returns a Imagine\Gd\Image object. This object can be used to save the new files.
$watermarkImage = '#webroot/../images/watermark.png';
$image = '#webroot/../slike/img-4.jpg';
// Store the Image object in a variable
$newImage = Image::watermark($image, $watermarkImage);
// Call the save function to write the file to the disk.
$newImage->save(Yii::getAlias('#webroot/../slike/img-4-watermark.jpg'));
Related
I have two function in two separate class, both have some Imagick processing.
I want the first Imagick to resize image and then send it to another Imagick instance without saving it as a file.
In the first class crop
function cropimage($inputfile){
$image = new Imagick($inputfile);
$image->cropimage($w, $h, $x, $y);
# and here I wish to return some $image->output???() which let me to use it in second class
return ????;
}
And in second class resize
function resizeImage(){
$crop = new crop(); // my first class instance
$image_to_work = $crop->cropimage($this->image); // output from first class
$image = new Imagick($image_to_work);
$image->resizeImage($width, $height);
$image->writeImage($outputfile);
}
Any ideas?
Thanks in advance.
Cheers
Images can be passed around as variables:
function cropimage($inputfile) {
$image = new Imagick($inputfile);
$image->cropimage($w, $h, $x, $y);
return $image;
}
function resizeImageAndSave($image) {
$image->resizeImage($width, $height);
$image->writeImage($outputfile);
}
$image = cropimage("./test.png"); // return the image
resizeImageAndSave($image); // pass it into the next function
Use sessions.
First, make sure you have session_start() in every script that will utilize sessions.
Then do something like $_SESSION['image'] = $image and then your next script can access that session variable.
I am trying to resize and replace an image uploaded by the user but the most I can do is resize and output as another file. I have used the library image magician to resize.
If someone can explain how I can do it without using the library it would be better.
public function add() {
$f=Base::instance()->get('FILES');
$fext=pathinfo ($f['usrimg']['name'],PATHINFO_EXTENSION);
$this->copyFrom('POST');
$this->save();
$newName=str_pad($this->_id,5,"0").'.'.$fext;
move_uploaded_file($f['usrimg']['tmp_name'], $newName);
$this->load('id='.$this->_id);
$this->set('photo',$newName);
$this->update();
require_once('php_image_magician.php');
$magicianObj = new imageLib($newName);
$magicianObj->resizeImage(100, 200);
$magicianObj->saveImage('q.jpg', 100);
}
If you want to use gd library, for example you can use this code for resizing jpeg images:
$image=file_get_contents('image.jpg');
$src=imagecreatefromstring($image);
$x=imagesx($src);
$y=imagesy($src);
$y1=100; //new width
$x1=intval($y1*$x/$y); //new height proprtional to new width
$dst=imagecreatetruecolor($x1,$y1);
imagecopyresized($dst,$src,0,0,0,0,$x1,$y1,$x,$y);
header("Content-Type: image/jpeg");
imagejpeg($dst);
I'm creating an API in which I have a model called OnlineAds(), it has these columns title, full_discription, short_discription and image, I also created an input form to fill in these fields and upload an image, in the image field I add the image's name and store the image in this path: storage/uploads. I was able to retrieve the image and display it, but I still didn't figure out how to resize it before displaying (lets say I want it to be 300 x 300), can anybody show me how ?
Note: I tried to use Stapler and Imagine but they seemed to give multiple bugs and failed!
This is my controller:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$filetype = File::type($path);
$response = Response::make(File::get($path), 200);
$response->header('Content-Type', $filetype);
return $response;
}
}
You could look into using Intervention to take care of the image processing as well as handling the response. It can be as easy as this:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$img = Image::make($path);
$img->resize(300, 300);
return $img->response();
}
}
There are detailed instructions on how to install the Intervention library and integrate it with Laravel, and it shouldn't take more than a few minutes to get it up and running.
I have created two imagick objects $obj_one add $obj_two and loaded image from file test.pdf into $obj_one.
Now i need to load image from $obj_one into $obj_two pretty much the same as from the file, but from another imagick object.
How can i do it?
Note i set $obj_two resolution twice as much than $obj_one and i need image to resize upon loading to $obj_one.
EDIT:
$obj_one = new Imagick();
$obj_two = new Imagick();
$obj_one->setOption('pdf:use-cropbox', 'true');
$obj_one->readImage("docs/test.pdf");
$output_x_res = 800; //px
$output_y_res = $obj_one->getImageHeight() * $output_x_res / $obj_one->getImageWidth();
$Img_Dpi_Arr = $obj_one->getImageResolution();
$final_x_dpi = ceil(($Img_Dpi_Arr['x'] / $obj_one->getImageWidth()) * $output_x_res);
$final_y_dpi = ceil(($Img_Dpi_Arr['y'] / $obj_one->getImageHeight()) * $output_y_res);
$obj_two->setResolution($final_x_dpi,$final_y_dpi);
...
I have .jpg images in path (/home/folder1/folder2/) to deny public access to the images. I'm using CodeIgniter and following function to show single image.
function show_image()
{
$image_path = '/home/folder1/folder2/photo2.jpg';
$image = file_get_contents($image_path);
header("Content-Type: image/png");
echo $image;
}
I would like to have solution where I can pass the resized image to CodeIgniter view and show it using tag.
function show_image2()
{
// TODO image load and resize
// loaded and resized image
$data['image'] = $image;
$this->load->view('show_image', $data);
}
View:
<img src="<?php echo $image; ?>" />
Do I need to make temporary copy of image to public path?
EDIT:
How can I make temporary copy of image? Source folder is "/home/folder1/folder2" and destination folder is "example.com/images". Image should be deleted when user changes the image.
Yes you do, unless you're passing the function the raw image data (which means you'll need to change show_image() a bit :)