Display resized image programatically without creating another image on server - php

Is there any library or script which would allow me to display the resized version of an image programmatically? without actually creating another resized image and saving it into the server?
I am looking for something like this, ie:
<img src="script.php?image_path=/mysite.com/public_html/img/my_image.jpg&new_width=300&new_height=100" />
Is it possible? Does it exist?
I hope I am making sense here...
Thank you very much for your help! :o)

You can use Cloudinary to programmatically resize images based on the resolution of the viewing device. All you need to do is basically mention the height and width parameters and it transforms the image on-the-fly:
<?php echo cl_image_tag("my_image.jpg",
array("width" => 100, "height" => 150, "crop" => "fill"));
?>
For more examples on re-sizing that suit your specific use-case, you can refer the the documentation here:
http://cloudinary.com/documentation/php_image_manipulation#resize_crop_and_thumbnails

Related

How set hover for featured image? How change hover?

I would like to set the gif file in the featured image. I would like it to run when I hover over it with the mouse.
I know I should set this in functions.php file
Together with the code:
add_theme_support ('post-thumbnails');
Unfortunately, I have no idea how to do it. I create my blog and I have never done such things. Could someone help me how to do it?
Thank you for your help!
When you upload an animated GIF to WordPress, it does all of it’s resizing magic to make the various thumbnail sizes (defaults are thumbnail, medium and large, in addition to the original full). When it does this, the resized versions lose their animation.
If your theme displays featured images at any size other than full, you’ll thusly lose your GIF’s animation.
If you definitely know that you want to display the animated version, though, there’s a little trick you can use.
<?php $thumb_url = get_the_post_thumbnail_url();
$thumb_low = strtolower($thumb_url);
if (strpos($thumb_low, '.gif') === false) {
$thumb_size = 'large';
} else {
$thumb_size = 'full';
}
?>
What am I doing here is getting the URL of our featured image, then I make sure it’s all lower case letters (WordPress may do this anyway, but just to be safe), then I check to see if .gif is present in the thumbnail’s URL, and finally, if it is, I set the thumbnail size I use to ‘large’. Doing so will allow me to use the original image, which retains it’s animation.
Then, I just need to tell our call to the_post_thumbnail() to use the variable thumbnail size we set, like this:
the_post_thumbnail($thumb_size)
If you’ve ever tried to set an animated gif as a featured image in WordPress, you’ve probably discovered that you can’t.
The reason why is pretty simple. Your theme grabs a resized version of the featured image you upload. And in the case of an animated gif, that means it’s not grabbing the original gif image, and so you end up with a static image.
The way around this is pretty simple, however. You can just install a plugin that automatically sets the featured image from the first image in a post. Of course that means you will need to make the animated image the first image, but at least it’s a workaround.
There are a number of plugins that will let you automatically set your first image as the featured image. I tried two and the both worked: Autoset Featured Image and Automatic Featured Image Posts.
Something else I found was that I didn’t even need to download the gif. I could just insert it with the URL.

Full image instead of thumbnail

We have a shop page on this website.
I will explain my problem using the very first image in the top left as example, although it applies to all of them.
If you hover over the first image (Gentes Deluxified English) you see an incomplete picture like this:
src= ".../Gentes-board-DLX-20180123-200x267.jpg"
This image actually cuts of parts of the images to the left and right. The full image is like this:
src= " .../Gentes-board-DLX-20180123.jpg"
So we want to change the thumbnail image to the full image, or keep the original ratio of the image. This needs to work for all images and the ones added in the future.
Is this fixable with css?
I already spent a lot of time looking at the source code of woocommerce to find the file that creates the page that uses the thumbnail images. I can't find that file. Where should I look for the source code files? And what should I change in these source files?
I looked at the source code of Woo commerce and found the parameter for cropping thumbnails for you.
https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-regenerate-images.php
Add this
$crop = false;
after line 318:
private static function get_image( $fullsizepath, $thumbnail_width, $thumbnail_height, $crop ) {

Better way Create Image Thumbnails Using PHP

I need to three size of images uploads for display in my page: 1-large 2-medium 3-small now i have Two choices and two way .
1 - when upload images, PHP GD Library generate three size of images and put in any folder : example : in /images/ folder
test-larg.jpg
test-medium.jpg
test-small.jpg
And Display :
<img src="/images/test-small.jpg" alt="">
2 - i upload original images and put in any folder. then, for each size (large/medium/small) generate image using GD library from images. example : TimThumbs / phpthumbs() etc...
method :
<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">
better way ? better choices ? Thanks.
My recommendation is to combine the two approaches:
On image upload, do nothing (apart from maybe adding the image metadata to a databse if so desired)
On a call to "thumbnail.php?src=/images/whatever.jpg&h=150&w=150&foo=bar" see, if you already have this image in this size - if yes, give it back, if no create it and store it as a file
This means,
you only create images you really need, thus saving CPU and storage
you have to clean up your cache, if you delete/replace an image

WordPress Post Thumbnail Resizing Issues

I have recently developed a website in WordPress, I have a few thumbnail sizes defined by add_image_size();
For example if you look here: http://bit.ly/kSTU0Q
Images in the right hand channel at the very bottom under 'MORE PRODUCT NEWS' we have this defined for
the_post_thumbnail()
add_image_size( 'side-excerpt', 86, 93);
So would be:
the_post_thumbnail( 'side-excerpt',
array('class' => 'alignleft') );
If you look WordPress does not seem to adhere to my sizing specifications and I have used the 'Regenerate Thumbnails' plugin and this seems to make near as no difference.
Hopefully you guys can shed some light on the situation.
Thanks in advanced!
Looks to me like your issue is with your crop style.
add_image_size has a third property that is crop style and defaults to false (soft proportional cropping.
"Box resizing shrinks an image proportionally (that is, without distorting it), until it fits inside the “box” you’ve specified with your width and height parameters."
The reason that your regenerate thumbnails is not working correctly, is because you are using this soft mode, and it seems like the result you are actually looking for is hard crop.
"in this mode, the image is cropped to match the target aspect ratio, and is then shrunk to fit in the specified dimensions exactly."
so you will want to change
add_image_size( 'side-excerpt', 86, 93);
to
add_image_size( 'side-excerpt', 86, 93, true);
For more information see:
http://markjaquith.wordpress.com/2009/12/23/new-in-wordpress-2-9-post-thumbnail-images/
http://codex.wordpress.org/Function_Reference/add_image_size

Is it possible to change image position and width with php GD?

I have a gif file with many icons and buttons on it..
What I want to do is include that gif file using php gd somefunction("file.gif")
then, resize the new image to 30px by 30px.. and then be able to position (using x and y coordinates) the actual gif file, so that only a certain area of the image shows on the new file..
just like the css background-position property but, with gd.
Thanx
imagecopyresized() resizes the image, so it doesn't quite work for what I was exactly looking for.
For anyone else that might need this, the best function is actually imagecopy().
http://www.php.net/manual/en/function.imagecopy.php
It will do the trick..
Thanx Trufa for the quick answer though.
imagecopyresized()
http://www.php.net/manual/en/function.imagecopyresized.php
Should do the trick for adjusting the size.

Categories