Better way Create Image Thumbnails Using PHP - 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

Related

PHP::Imagick Creating a Temp FIle from A Composite Image, for displaying through <img> Tag

Is it possible to create a temp file server side for viewing an image?
I've some images & they're displayed from server but I want to have them watermarked... Found out about the Imagick Class, used composite... It can be viewed when sent as header but I need it somehow visible for the tag.
I don't want to uses 'write image' & create a new image from the composited image. I'm trying to use it as client Side thing (I'm not familiar with JS... Yes, I'll look into it but need an alternative till then).
Another alternative that I'm aware of is I should control that WHILE uploading the images but I've already uploaded quite a few so...
Any help appreciated!
Have you tried using the function that generates your image as a src attribute for the <img /> tag?
I don't know exactly how your code is, but I am thinking the end result could be something like:
<img src="data:image/jpeg;base64,<?php echo base64_encode($image_headers); ?>">
You basically output the headers inside the image src as a base64 encoded string.
What you should keep in mind is that you should adjust the image/jpeg type to the corresponding type of your image.
For example:
For JPG, JPEG files it would be image/jpeg
For PNG files it would be image/png
For WEBP files it would be image/webp
And of course the encoded string should be an image of the corresponding format.

Storing Images In a PHP File

I have been trying to change the images in the WampServer Index.php File... When I looked at the Index.php File I saw something Interesting. The File its self was containing the Image in the RAW format. Latter in the Code the PHP Script Calls the Image using the URL like http://localhost/index.php?img=pngFolder called a Image file stored RAW in the PHP file as a png.
Here is a link to a website that has the index.php code...
Link
I would Like to know how to replicate this same process to work for other images. Granted the File will be larger but its a Price to pay for what I am doing for a project. The Reason I want some help with this is so I can do it correctly. I have Tried 2 times already. I managed to get it to call one image correctly but not some of the others. I'm not sure if the image is just a different encoding or what..... Any Help would be Appreciated.
They are using BASE64 to encode the images into text. You can google for a base64 encoder that will convert your images to text. You can then put the text directly in an <img src="..base64 text.." />
Here's one..
https://www.base64-image.de/
As far as getting the image from the url index.php?img=pngfolder..
You could put this at the top of the file
if(isset($_GET['img'])){
echo "...base64 string.."; exit;
}
Then you can use the index url as the src for your image and it will simply retrieve the base64 image

Linking images regardless of file type

I'm building a little site at the moment and users can upload their own profile picture but obviously some users pictures will be png and some jpeg, does anyone know anyway to be able to accept both when using the images as a background image in css. Here is my HTML:
<div class='avatar' style='background-image: url('users/avatars/$username.jpg')'></div>
Inside the users/avatars/ folder is every users profile picture named like so: "username.jpg" or "username.png".
Here is an example of my issue: http://cl.ly/image/3W311S2v0v0t
The first post loads the users profile image because it is a jpeg but the second post hasn't loaded the profile image as it is a png.
Thanks in advance.
You can do it in CSS like this:
background-image: url('users/avatars/$username.jpg'), url('users/avatars/$username.png');
If you absolutely must keep all the logic on the client side, in Javascript, you can catch the error if the image fails to load and attempt to load the other version.
For example:
<div class='avatar'>
<img src="users/avatars/$username.jpg" onerror='this.src="users/avatars/$username.png";' />
</div>
So if the JPG is not found and returns an error, the image will then load the PNG version.
If you are using PHP you can check the file extension and serve up different HTML. Here's how to read the file extension:
How to get the file extension in PHP?

does this affect speed or should i go with normal method of displaying image

i am displaying an image usign readfile() function of php
HMTL:
<img src='image.php?id=232'/>
PHP: image.php
<?php
$id=$_GET["id"];
if(image_view_allow($id)){
$path=get_image_path($id);
readfile($path);
}else{
readfile("images/not_allow.png");
}
image_view_allow and get_image_path is two that i have defined to check validation and get path
I am doing this because i want show image only to the allow person.
Does this affect speed of downloading an image ?
what is normal(means direct path in src attribute of img tag) or trick that is shown above?
Just loading an image probably same,but it is always better if you handle images with php, because when you resize image with php you load the needed size. But with html you load the bigger size than you need and resize.
first -> Does this affect speed of downloading an image ?
answer: no, because when the page is loaded, the php code is already translated to html before page is loaded in the borwser.
second -> what is normal(means direct path in src attribute of img tag) or trick that is shown above?
answer: both are same as both involve php code. When code executes in either way it will enter the source of the image in the image tag.

Combine two images (.JPG) using PHP GD

I can't find a solution to this. I want to add 20px empty space to this image:
http://img233.imageshack.us/img233/419/78317401.jpg
and then paste this watermark at the bottom (on the blank space)
So the output would be:
http://img252.imageshack.us/img252/4554/wynik.jpg
I don't want to stretch it.
EDIT
Did it with WIdeImage. Rly simple.
1) Load both images with
http://www.php.net/manual/en/function.imagecreatefromjpeg.php
2) Get 1st image height and width with
http://www.php.net/manual/en/function.imagesy.php
http://www.php.net/manual/en/function.imagesx.php
3) Create larger image with height+20
http://www.php.net/manual/en/function.imagecreatetruecolor.php
4) Copy the first image and the second image to the larger image
http://www.php.net/manual/en/function.imagecopy.php
5) Save it
http://www.php.net/manual/en/function.imagejpeg.php
6) Done
Try wide image api http://wideimage.sourceforge.net/
Check out one of their this demo may be that help you
Merge and
Resize

Categories