I want to resize an image twice using Intervention.
I have this currently:
$img = Image::make($image_url);
$img_path = public_path() . '/images/';
$img->fit(500, 250);
$img->save($img_path . '/img_250.jpg');
$img = Image::make($image_url);
$img->fit(100, 100);
$img->save($img_path . '/img_100.jpg');
As you can see, I first want to resize the original image to 500x250, and then I want to again resize the original image (not the 500x250 image) to 100x100.
Is there a way to do this without calling Image::make() twice?
Here's the answer:
http://image.intervention.io/api/reset
// create an image
$img = Image::make('public/foo.jpg');
// backup status
$img->backup();
// perform some modifications
$img->resize(320, 240);
$img->invert();
$img->save('public/small.jpg');
// reset image (return to backup state)
$img->reset();
// perform other modifications
$img->resize(640, 480);
$img->invert();
$img->save('public/large.jpg');
I'm posting this to help others who might come across a similar issue. While we can implement #user6421733's answer... There's a better way of handling different sizes of images.
Consider using Intervention's imagecache optional package. You could implement it simply too. http://image.intervention.io/use/url
It can allow you to use urls such as this http://yourhost.com/{route-name}/original/{file-name} and with little or less effort:
Related
I've searched everywhere for two days now, still no help.
my project uses jQuery Guillotine Plugin to allow users select image size and position before uploading to my php server but i can't figure out the proper way to crop and scale the image based on the data received from the frontend.
Here is what i've tried:
The response i'm trying to work with looks like this:
{ scale: 0.324, angle: 0, x: 110, y: 34, w: 300, h: 300 }
Then the php code:
$imagick = Image::make($destination . "/" . $fileName);
$height = $imagick->height();
$width = $imagick->width();
$imagick->rotate($req['angle']);
//using the data recieved after user selection
$imagick->cropImage((int)$req['w'], (int)$req['h'], (int)$req['x'], (int)$req['y']);
//Write image to disk
$imagick->save($destinationPathSmaller . $fileName);
At this point, the picture doesn't display correctly. i really don't know what to do, this is my 3rd day on this. please help!
Thanks in inadvance,
I'm not sure if it's just a different in copying and pasting but I think this may be a simple misusage of the library.
For one. You have this line:
$imagick = new Image($destination . "/" . $fileName);
But they suggest that you use
$imagick = Image::make($destination . "/" . $fileName);
The difference between the two is that a driver is not getting set if you simply call new Image. So the crop method simply bounces off the AbstractDriver class they have and does nothing. But you can call Image::make and it will return the driver.
If that doesn't seem to be the case then I also noticed that you're using writeImage. PHP Interventions method for creating a file based on changes is ->save();. I couldn't find the method writeImage anywhere.
Okay, so i finally got it working.
I thank everyone that tried to help, here is what i did in case someone else is faced with same issue:
Using Intervention and Imagick as the driver.
After collecting your data from the guillotine API, process it and use it like this.
$imagick = Image::make($destination . "/" . $fileName);
$width = $imagick->width() * $req['scale'];
$imagick->rotate($req['angle']);
$imagick->widen($width);
//Crop to desired width and height.
//Note: the width and height has to be same with what you set on your Guillotine config when instantiating it.
$imagick->crop($req['w'], $req['h'], $req['x'], $req['y']);
//Finally, Save your file
$imagick->save($new_destination . $fileName);
That should just work perfectly.
I want to crop/resize an image in Laravel (4.2). But it doen't seem to work very well...
So I Want to be able to upload an image, but resize it to a maximum width and height.
How should I do this?
The code I have now is:
//save the image
$destinationPath = 'public/pictures/news';
if (Input::hasFile('img'))
{
$file = Input::file('img');
$file->move('public/pictures/news', $file->getClientOriginalName());
}
So, I want a code in the if() statement, that crops the image. to a specified max-width and max-height.
If someone could help me out, I would be very happy!
Kindes regards,
Robin
use image intervention package it is providing an easier and expressive way to create, edit, and compose images.
https://packagist.org/packages/intervention/image
I am storing all the images in a folder. These images are uploaded by the user through his account. Hence, the images are of different sizes.
I want to display the images in 2 div elements with a fixed width and height (but different from each other) . The sizes may be of the order 40*40 pixels and 200*200 pixels. What would be the better way to do it -
1) Storing images of different sizes in the folder while uploading at the first place, or
2) Using the 'height' and 'width' attributes in img tag in HTML to display the image in the correct size.
Or is there some other way?
Thanks in advance.
Of course it's better to store images already resized and cropped.
If you want to have multiple images with different dimensions .
It will send less data across.
The better way is too store all sizes of all images.In that case it works much faster.
original/test.jpg
40x40/test.jpg
200x200/test.jpg
Also please read this article
http://selbie.wordpress.com/2011/01/23/scale-crop-and-center-an-image-with-correct-aspect-ratio-in-html-and-javascript/
The best way is to store the original image and request the correct sized image from the client. At that time if the correct size image is not found, create it and add it to a cache folder. Then, deliver the correct size image from the cache.
So if image.png of size 1200x800 is uploaded, store that in original_images folder.
Then, construct a php script sized_image.php and use it in your HTML like this
<img src="sized_image.php?img=image.png&height=200&width=200" />
In your sized_image.php script you would do the following
$fileName = "cached_image/{$_GET['width']}x{$_GET['height']}_{$_GET['img']}";
if (!file_exists($fileName))
{
//resize and store in $fileName
}
$type = 'image/png'; //Or whatever type the image is
header('Content-Type:' . $type);
header('Content-Length: ' . filesize($fileName));
readfile($fileName);
exit();
Iam working on a project , which involves fetching images from various websites and displaying a Thumnail or resized version of orignal image.
As i will be fetching many images at time , this takes more time , As Orignal image files will be loaded.
What i need is given an image url , i need resized version of that image file? So that i need not download the Large orignal image file...
for example : orignal image : 500X800 some 500kb
what i need is : 100X150 some 100kb image file..
Is it possible using Jquery? OR PHP?
Are their any functions like imagecopyresampled(PHP) in Jquery or any plugins?
Well, the big file needs to be downloaded at one point to create the thumbnail.
What you can do, through JQuery, is ask your server for the thumbnail, the server downloads the file, creates the thumbnail and sends back to the client the URL of the thumbnail when the resizing is done. This way, the client will gradually receive thumbnails as they are ready.
Edit After experimenting a bit, I found out that as soon as an img requests a picture, even if you dynamically remove its src attribute, the download continue.
So I decided to write a sample code (there's not much validation and it only works for jpeg, adding the other types would be really easy though). The solution is divided in 3 parts:
HTML
First, I used an empty div with some specific attributes to let jQuery what to load. I need to apologize to Nuria Oliver, she had the first "big picture" I could find on the net. So, here's a sample div:
<div class="thumbnail" data-source="http://www.nuriaoliver.com/pics/nuria1.jpg" data-thumbnail-width="100" data-thumbnail-height= "200"/></div>
I use thumbnail as class to be able to find easily the divs I want. The other data parameters allow me to configure the thumbnail with the source, width and height.
JavaScript / jQuery
Now, we need to locate all these divs requesting thumbnails... using jQuery it is pretty easy. We extract all the data settings and push them in an array. We then feed a PHP page with the query and wait for the response. While doing so, I'm filling the div with some HTML showing the "progress"
<script type="text/javascript">
$(".thumbnail").each(function() {
var img = $(this);
var thumbnailWidth = img.data("thumbnail-width");
var thumbnailHeight = img.data("thumbnail-height");
var thumbnailSource = img.data("source");
var innerDiv = "<div style='width:" + thumbnailWidth + "px; height:" + thumbnailHeight + "px'>Loading Thumbnail<br><img src='loading.gif'></div>";
img.html(innerDiv); // replace with placeholder
var thumbnailParams = {};
thumbnailParams['src'] = thumbnailSource;
thumbnailParams['width'] = thumbnailWidth;
thumbnailParams['height'] = thumbnailHeight;
$.ajax({
url: "imageloader.php",
data: thumbnailParams,
success: function(data) {
img.html("<img src='" + data + "'>");
},
});
})
</script>
PHP
On the PHP side of things, I do a quick test to see if the picture has already been cached (I'm only using the file name, this should be more complicated but it was just to give you an example) otherwise I download the picture, resize it, store it in a thumbnail folder and return the path to jQuery:
<?php
$url = $_GET["src"];
$width = $_GET["width"];
$height = $_GET["height"];
$filename = "thumbnail/" . basename($url);
if(is_file($filename)) // File is already cached
{
echo $filename;
exit;
}
// for now only assume JPG, but checking the extention should be easy to support other types
file_put_contents($filename, file_get_contents($url)); // download large picture
list($originalWidth, $originalHeight) = getimagesize($filename);
$original = imagecreatefromjpeg($filename); // load original file
$thumbnail = imagecreatetruecolor($width, $height); // create thumbnail
imagecopyresized($thumbnail, $original, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight); // copy the thumbnail
imagedestroy($original);
imagejpeg($thumbnail, $filename); // overwrite existing file
imagedestroy($thumbnail);
echo $filename;
Browser
So, what does it do exactly? In the Browser, when I open the page I first see:
And as soon as the server is done processing the image, it gets updated automatically to:
Which is a thumbnail version, 100x200 of our original picture.
I hope this covers everything you needed!
PHP Thumb is a good plugin it worked for me and easy to operate.
And the best part is there are many options to select about the output file like its quality, max width for potrait images , max width for landscape images etc..
And we can set permissions like block off server requests, block off domain thumbnails.. etc
Note: this was actually posted by someone , as an answer to this question..But it was deleted.So iam reposting it, as it was useful.
for this to work you need to do some actual image processing which you cant do with js, so some server side language is needed.PHP is an option
If you are not interesting in obfuscating the image source, you may very well rely on an external tool like http://images.weserv.nl/ which takes an original image and allows you to request it in a different size.
If you end up not relying on a global CDN, at least sort out proper caching. Last thing you want to do is resize the same image over and over again for subsequent identical requests - any benefit of image's lower size will probably be negated by processing time.
When users input their images, their images are any size. I want to be able to resize all the images to a specific dimension. Is there a function that allows me to do that in PHP?
thanks
The function you need is imagecopyresampled, that also interpolate pixels, (imagecopyresized does not);
In my code I use a it in a function like this:
function resizeAndSavePhoto($original, $destination, $dest_width, $dest_height){
$photo = createImage($original);
$size = getimagesize($original);
$final_photo = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($final_photo, $photo,0,0,0,0,$dest_width, $dest_height, $size[0], $size[1]);
imagejpeg($final_photo, $destination, 100);
}
$orignal and $destination are filename paths
http://php.net/gd
Specifically, http://php.net/imagecopyresized
Try ImageMagick, it keeps the EXIF information in an image if it needs it, among other things:
http://php.net/manual/en/book.imagick.php
ok you can make something:
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
or use something already made, i use this one:
http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
which works a treat
Here's a very good library that you can resize images with. It has ways to resize them so they are still proportionate and it has options to save, or display images as well. Works really slick
http://phpthumb.gxdlabs.com/