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

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.

Related

PHP thumb resize png image but add black background

I am using PHPThumb to resize images rumtime. It is working fine but it add black background on PNG image.
How could I resolve this?
my path is
phpThumb/phpThumb.php?src={imagepath}&w=95&h=92
Here is the example to avoid black background:
src="/uploads/phpThumb.php?src=images/expressionengine_logo.png&w=100&f=png"
src="/uploads/phpThumb.php?src=images/expressionengine_logo.png&w=100&bg=D4E9F7"
Well, I am not familiar with PHPThumb but you can use SimpleImage library with this update.
I am using it and didnt faced any problems yet. This library has purposefully updated for the problem you are facing right now. Go to the links and try to use it.
Try replacing ImageCreateFromJPEG($file) with ImageCreateFromPNG($file)
Most probably this is the common error for the black background in the resized pngimage

Using 'convert interlace -line' on animated .gif breaks image

Before I explain, you may want to take a quick look at my thumbnail generation code: http://pastebin.com/XAPPcUyZ
I pass several image types through this script: jpeg,jpg,gif,png
All types create a 220px wide thumbnail like they should, the problem arrises when the script runs into an animated .gif image - when attempting to make a thumbnail for the gif it outputs something like this: http://i.imgur.com/oh50z.gif where the image appears tiled when a proper thumbnail should look more like this: http://whatimg.com/i/mhsnf4_thumb.gif.
If I remove 'interlace -line' the animated gif's generate thumbnails without any issue, however I would like to leave this in so that the other file formats are interlaced for faster loading. Is there a way to run interlace without breaking animated gifs like this? If there isn't how can I go about detecting if a gif is animated and running a non-interlaced convert on it instead?

How to make image's background transparency to 0?

There are images displayed inside a HTML table in a PHP code :
The code of this table is :
for ($i = 0; $i < $data['list']['cnt']; $i++) {
$tabRows[$i][1]['width'] = "45%";
$tabRows[$i][1]['align'] = "center";
$tabRows[$i][1]['value'] = '<img src="'.HTTP_FIGURES.$data['list'][$i]['fig_image'].'" />';
}
As you can see the background of the images are seen and they make the page dirty. So I want to remove the background of each image. How to do that ?
Make the images a transparent PNG.
Your only practical solution is to go through each image with an image editor, delete the background and re-save as a .png with transparency.
Take any image editor like GIMP (free), Paint.net(free), Photoshop or any other image editor and add transparency where you need it. Here is a tutorial for paint.net. If your images are not in PNG - you will need to make PNG images as JPEG has no transparency.
You have 2 options:
Edit images with a image editor like paint.net and make the backgrounds transparent.
Use the PHP GD functions to 'edit' the images. This can be cumbersome, because you have to determine what color you want to replace with a background color. Often, a fixed color is used, or the color in pixel[1,1].
My advise, if there are not to many images, go for 1.

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

PHP crop and resize image on the fly

I have a web page that displays images that I don't know their size in advance. I was trying to use the GD functions to make the script resize and crop the images from me " Just before they are displayed.. I don't need caches" but I failed.
I need a script that I can call like this
<img src="display.php?src=blablabla&height=100&width=200" ?>
or even by calculating the width and height of css to preserve the proportions and make the image touch the box from inside like
<img src="blabla.jpg" style="height:<?php echo $height; ?>; width:<?php echo width; ?>" />
I don't need any sort of caching. How can I do that ?
WideImage rlz! :)
The resize's like that:
header('Content-type: image/jpeg');
echo WideImage::load('image.jpg')->resize(200, 100)->asString('jpg', 80);
// image.jpg resized at 200x100 with 80% of quality
You'll need to use the first style. Because this would be happening server-side, you can't check the CSS to get the desired size.
You just need to use the GD functions to open the appropriate file, use imagecopyresampled() to resize it, and then output to the buffer using imagejpeg. Don't forget to set the right headers:
header('Content-type: image/jpeg');
OR phpthumb http://phpthumb.sourceforge.net/
Demo is available at: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php
You are looking for TimThumb (Demo | Source Code):
Simply copy the source code into a new
document called ‘timthumb.php’, place
it in a folder on your site (ex:
/scripts/) and call the image like
this:
<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">

Categories