Create Image size (jpg)

Source: Internet
Author: User
Tags apache log file image identifier imagemagick apache log
Imagecopyresized (PHP3, PHP44.0.0) imagecopyresized -- encode (resourcedst_im, resourcesrc_im, intdstX, intdstY, intsrcX imagecopyresized
(PHP 3, PHP 4> = 4.0.0)

Imagecopyresized -- Copy and resize part of an image
Description
Int imagecopyresized (resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int DTH, int srcW, int srcH)


Imagecopyresized () copies a rectangular portion of one image to another image. dst_im is the destination image, src_im is the source image identifier. if the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be Med. the coordinates refer to the upper left corner. this function can be used to copy regions within the same image (if dst_im is the same as src_im) but if the regions overlap the results will be unpredictable.

See also imagecopyresampled ().

User Contributed Notes
Imagecopyresized
Plusplus7@hotmail.com
25-Sep-1999

Saw this one on the ng and it works fine-just trying to figure it out
Jpg.

$ New_w = 100;
$ New_h = 100;

Header ("Content-type: image/gif ");

$ Dst_img = ImageCreate ($ new_w, $ new_h );

$ Src_img = ImageCreateFromGif ("./imgtest.gif ");

ImageCopyResized ($ dst_img, $ src_img, 0, 0, 0, $ new_w, $ new_h, ImageSX ($ src_img), ImageSY ($ src_img ));

ImageGif ($ dst_img );

?>



Hoiarie@hotmail.com
15-Jul-2000

If you want to resize or copy an image from or to a datbase, just use
Temp-file as buffer. E. g.: copy all the image-data from the database to
Tempfile, use the resize-function on another tempfile and copy all
Data from to tempfile back into the database.



Skurrilo@skurrilo.de
28-Nov-2000

If you aren't happy with the quality of the resized images, just give
ImageMagick a try. (http://www.imagemagick.org) This is
A commandline tool for converting and viewing images.



Jernberg@fairytale.se
15-Feb-2001 07:02

[Ed. note: if you're using GD 2.x, you can use imagecopyresampled () which
Does exactly the same thing]

Here is my addition to imagecopyresized

It implements bicubic interpolation of pixels so the result is much better
Than the ordinary imagecopyresized...

Copy it into the src/ext/gd/gc. c and recompile the lib.
I wish someone cocould do that so it's stored in the "official"
Release

It's a really good function to scale down pictures to thumbnails.

/* {Proto int imagecopyresizedbicubic (int dst_im, int src_im, int
Dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w,
Int src_h)
Copy and resize part of an image */
PHP_FUNCTION (imagecopyresizedbicubic)
{
Zval ** SIM, ** DIM, ** SX, ** SY, ** SW, ** SH, ** DX, ** DY, ** DW, ** DH;
GdImagePtr im_dst, im_src;
Int srcH, srcW, DTH, dstW, srcY, srcX, dstY, dstX;
Int I, j;
Int r, g, B, c1, c2, c3, c4, color;
Float sX, sY;
Float scaleX, scaleY, scaleX2, scaleY2;
GDLS_FETCH ();

If (ZEND_NUM_ARGS ()! = 10 |
Zend_get_parameters_ex (10, & DIM, & SIM, & DX, & DY,
& SX, & SY, & DW, & DH, & SW, & SH) = FAILURE)
{
WRONG_PARAM_COUNT;
}

ZEND_FETCH_RESOURCE (im_dst, gdImagePtr, DIM,-1, "Image ",
Le_gd );
ZEND_FETCH_RESOURCE (im_src, gdImagePtr, SIM,-1, "Image ",
Le_gd );

Convert_to_long_ex (SX );
Convert_to_long_ex (SY );
Convert_to_long_ex (SW );
Convert_to_long_ex (SH );
Convert_to_long_ex (DX );
Convert_to_long_ex (DY );
Convert_to_long_ex (DW );
Convert_to_long_ex (DH );

SrcX = Z_LVAL_PP (SX );
SrcY = Z_LVAL_PP (SY );
SrcH = Z_LVAL_PP (SH );
SrcW = Z_LVAL_PP (SW );
DstX = Z_LVAL_PP (DX );
DstY = Z_LVAL_PP (DY );
Dsomething = Z_LVAL_PP (DH );
DstW = Z_LVAL_PP (DW );

// Kopiera paletten

For (I = 0; I <256; I ++ ){
GdImageColorAllocate (im_dst, im_src-> red [I], im_src-> green [I],
Im_src-> blue [I]);
};

// GdImageCopyResized (im_dst, im_src, dstX, dstY, srcX, srcY, dstW, DTH,
SrcW, srcH );

ScaleX = (float) (srcW-1)/(float) dstW;
ScaleY = (float) (srcH-1)/(float) dsomething;

ScaleX2 = scaleX/2.0f;
ScaleY2 = scaleY/2.0f;

For (j = 0; j for (I = 0; I
SX = (float) I * scaleX;
SY = (float) j * scaleY;

C1 = gdImageGetPixel (im_src, (int) (sX), (int) (sY + scaleY2 ));
C2 = gdImageGetPixel (im_src, (int) (sX), (int) (sY ));
C3 = gdImageGetPixel (im_src, (int) (sX + scaleX2), (int) (sY + scaleY2 ));
C4 = gdImageGetPixel (im_src, (int) (sX + scaleX2), (int) (sY ));

R = (im_src-> red [c1] + im_src-> red [c2] + im_src-> red [c3] +
Im_src-> red [c4])/4;
G = (im_src-> green [c1] + im_src-> green [c2] + im_src-> green [c3]
+ Im_src-> green [c4])/4;
B = (im_src-> blue [c1] + im_src-> blue [c2] + im_src-> blue [c3] +
Im_src-> blue [c4])/4;

Color = gdImageColorClosest (im_dst, r, g, B );
GdImageSetPixel (im_dst, (I + dstX), (j + dstY), color );

};
};

RETURN_TRUE;
}




# Endif/* HAVE_LIBGD */



Aleczapka@gmx.net
13-Mar-2001 :33

Re: imagecopyresizedbicubic

Yes, this function is really great! The thumbnails are much, much better.
However the example posted abve is for PHP4, and if you need it for PHP3
(And you really shocould use it), you have to do the following:
(Notice: I changed the name of this function to imagecopyresizedbb-it's
Just shorter to write this way)

Edit the file functions/gd. c.

Find the function named "function_entry gd_functions []" and add
This function definition there: (I suggest below the orginal
Imagecopyresized-around line 98)


{"Imagecopyresizedbb", php3_imagecopyresizedbb, NULL },


Then add this function (I suggest below the orginal imagecopyresized
Function-around line 2030)

/* {Proto int imagecopyresizedbb (int dst_im, int src_im, int dst_x, int
Dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h );
Copy and resize (bicubic) part of an image */
Void php3_imagecopyresizedbb (INTERNAL_FUNCTION_PARAMETERS)
{
Pval * SIM, * DIM, * SX, * SY, * SW, * SH, * DX, * DY, * DW, * DH;
GdImagePtr im_dst;
GdImagePtr im_src;
Int srcH, srcW, DTH, dstW, srcY, srcX, dstY, dstX;
Int ind_type;
Int I, j;
Int r, g, B, c1, c2, c3, c4, color;
Float sX, sY;
Float scaleX, scaleY, scaleX2, scaleY2;
GD_TLS_VARS;

If (ARG_COUNT (ht )! = 10 |
GetParameters (ht, 10, & DIM, & SIM, & DX, & DY, & SX,
& SY, & DW, & DH,
& SW, & SH) = FAILURE ){
WRONG_PARAM_COUNT;
}

Convert_to_long (SIM );
Convert_to_long (DIM );
Convert_to_long (SX );
Convert_to_long (SY );
Convert_to_long (SW );
Convert_to_long (SH );
Convert_to_long (DX );
Convert_to_long (DY );
Convert_to_long (DW );
Convert_to_long (DH );

SrcX = SX-> value. lval;
SrcY = SY-> value. lval;
SrcH = SH-> value. lval;
SrcW = SW-> value. lval;
DstX = DX-> value. lval;
DstY = DY-> value. lval;
Dsomething = DH-> value. lval;
DstW = DW-> value. lval;

Im_src = php3_list_find (SIM-> value. lval, & ind_type );
If (! Im_src | ind_type! = GD_GLOBAL (le_gd )){
Php3_error (E_WARNING, "Unable to find image pointer ");
RETURN_FALSE;
}

Im_dst = php3_list_find (DIM-> value. lval, & ind_type );
If (! Im_dst | ind_type! = GD_GLOBAL (le_gd )){
Php3_error (E_WARNING, "Unable to find image pointer ");
RETURN_FALSE;
}

For (I = 0; I <256; I ++ ){
GdImageColorAllocate (im_dst, im_src-> red [I],
Im_src-> green [I], im_src-> blue [I]);
};

ScaleX = (float) (srcW-1)/(float) dstW;
ScaleY = (float) (srcH-1)/(float) dsomething;

ScaleX2 = scaleX/2.0f;
ScaleY2 = scaleY/2.0f;

For (j = 0; j
For (I = 0; I
SX = (float) I * scaleX;
SY = (float) j * scaleY;

C1 = gdImageGetPixel (im_src, (int) (sX), (int) (sY + scaleY2 ));
C2 = gdImageGetPixel (im_src, (int) (sX), (int) (sY ));
C3 = gdImageGetPixel (im_src, (int) (sX + scaleX2 ),
(Int) (sY + scaleY2 ));
C4 = gdImageGetPixel (im_src, (int) (sX + scaleX2), (int) (sY ));

R = (im_src-> red [c1] + im_src-> red [c2] +
Im_src-> red [c3] + im_src-> red [c4])/4;
G = (im_src-> green [c1] + im_src-> green [c2] +
Im_src-> green [c3] + im_src-> green [c4])/4;
B = (im_src-> blue [c1] + im_src-> blue [c2] +
Im_src-> blue [c3] + im_src-> blue [c4])/4;

Color = gdImageColorClosest (im_dst, r, g, B );
GdImageSetPixel (im_dst, (I + dstX), (j + dstY), color );
};
};

RETURN_TRUE;
}
/*}}}*/




Then add this line in functions/php3_gd.h (I suggest below the orginal
Imagecopyresized function-around line 74)

Extern void php3_imagecopyresizedbb (INTERNAL_FUNCTION_PARAMETERS );


Then recompile PHP and that's it!
Later in PHP script just use imagecopyresizedbb instead of standard
Imagecopyresized function and your thumbnails will become smooth and
Shiny.



Colin_Witt@baylor.edu
05-Apr-2001

One note on getting the ImageCopyResizedBicubic to work. Just adding
Code block to gd. c won't do it. You have to do two other things as well:

In the first 150 or so lines of gd. c, you will find a section that starts
Like this:

Function_entry gd_functions []

Find the line that starts PHP_FE (imagecopyresized, copy this line, and
Paste a new copy below the original. Change
"Imagecopyresized" to "imagecopyresizedbicubic" in
New copy.

Also, you will need to edit php_gd.h as follows:

Find the line that says:
PHP_FUNCTION (imagecopyresized );
(It was line 87 for me)
And add a line below it that says:
PHP_FUNCTION (imagecopyresizedbicubic );

Once you 've made these changes, recompile and reinstall php, and you
Shocould be good to go. Scaling works much better with this function.



Aleczapka@gmx.net
05-Apr-2001

Just wanted to point out that the post above applies to PHP4. As well
The one posted, 3 posts abve, concerning
"Imagecopyresizedbicubic ".

If you wowould like to use it with PHP3, read the post titled "re:
Imagecopyresizedbicubic ".



Darren@php4hosting.com
16-Jun-2001

OK after too then e-mails :)
Http://www.alt-php-faq.org/downloads/patch

Assuming you are using Linux & PHP-4.0.5 Source
Get PHP-4.0.5 Source
Wget http://www.alt-php-faq.org/downloads/patch
Tar-pxzf php-4.0.5.tar.gz
Cd php-4.0.5
Patch-p0 <../patch

Then do your normal configure/make install

Check PHPinfo under GD for ImageResizeBicubic enabled.



Sfm15@columbia.edu
05-Jul-2001

I 've been using the imagecopyresizedbicubic function and it works great
Making higher quality thumbnails. However, it seems that the thumbnails
Are very large with respect to memory. For instance, I'm resizing images
From 200x280 pixels to 140x195 pixels but the smaller pictures wind up
Being about 26 K while the original larger pictures are usually around 12 K.
Also, when I output the image as a JPEG, it makes no difference what I
Select as the quality. It comes out being the same size/quality
Regardless.



Rze@counter-strike.net
10-Jul-2001 11: 47

For those who can't update their GD modules, I 've made this function. If
You think you can make any good changes, feel free to e-mail them to me.

Frankly I have no idea what the for loop in the first example does, I just
Guessed and it seems to work fine.

Note: it's a VERY, VERY, VERY slow function. Never use it for dynamic
Output, only saving to a thumbnail file or the like.


Function ImageCopyResampleBicubic ($ dst_img, $ src_img, $ dst_x, $ dst_y,
$ Src_x, $ src_y, $ dst_w, $ dst_h, $ src_w, $ src_h)
/*
Port to PHP by John Jenkins July 10 2001 -- original code (in C, for
Php gd Module by jernberg@fairytale.se
*/
{
For ($ I = 0; $ I <256; $ I ++) {// get pallete. Is this algoritm
Correct?
$ Colors = ImageColorsForIndex ($ src_img, $ I );
ImageColorAllocate ($ dst_img, $ colors ['red'], $ colors ['green'],
$ Colors ['blue']);
}

$ ScaleX = ($ src_w-1)/$ dst_w;
$ ScaleY = ($ src_h-1)/$ dst_h;

$ ScaleX2 = $ scaleX/2.0;
$ ScaleY2 = $ scaleY/2.0;

For ($ j = $ src_y; $ j <$ dst_h; $ j ++ ){
$ SY = $ j * $ scaleY;

For ($ I = $ src_x; $ I <$ dst_w; $ I ++ ){
$ SX = $ I * $ scaleX;

$ C1 = ImageColorsForIndex ($ src_img, ImageColorAt ($ src_img, (int) $ sX,
(Int) $ sY + $ scaleY2 ));
$ C2 = ImageColorsForIndex ($ src_img, ImageColorAt ($ src_img, (int) $ sX,
(Int) $ sY ));
$ C3 = ImageColorsForIndex ($ src_img, ImageColorAt ($ src_img, (int) $ sX
+ $ ScaleX2, (int) $ sY + $ scaleY2 ));
$ C4 = ImageColorsForIndex ($ src_img, ImageColorAt ($ src_img, (int) $ sX
+ $ ScaleX2, (int) $ sY ));

$ Red = (int) ($ c1 ['red'] + $ c2 ['red'] + $ c3 ['red'] + $ c4 ['red'])/
4 );
$ Green = (int) ($ c1 ['green'] + $ c2 ['green'] + $ c3 ['green'] +
$ C4 ['green'])/4 );
$ Blue = (int) ($ c1 ['blue'] + $ c2 ['blue'] + $ c3 ['blue'] + $ c4 ['blue'])
/4 );

$ Color = ImageColorClosest ($ dst_img, $ red, $ green, $ blue );
ImageSetPixel ($ dst_img, $ I + $ dst_x, $ j + $ dst_y, $ color );
}
}
}



Darren@php4hosting.com
11-Jul-2001

This function works great with our installand is just as fast as well
Good quality but, it does not work with GD version 2!

Something to remember when upgrading



Info@methmedia.de
27-Sep-2001

Header ("Content-type: image/png ");
$ Width = implode ($ argv ,"");
$ Im1 = imagecreatefrompng ("timber.png ");
$ Im = imagecreate ($ width, 12 );
Imagecopyresized ($ im, $ im1, 0, 0, 0, 0, $ width, 12, ImageSX ($ im1 ),
ImageSY ($ im1 ));
ImagePng ($ im );
ImageDestroy ($ im );

Works only if the colordepth of the png-file is true color. I have tried
It with 256 colors and the picture was just black.



28--200-2001

When using this function along with imagecreatefromstring () you must use
ImageCreateTrueColor () for the destination image. This will give
Complete palette. Otherwise, the resulting image will look strange. This
Also applies to ImageCopyResampled ().



William@waw.com.br
12-Jan-2002

REDEFINE? BICUBIC? ARGH!

If you aren't happy with the quality of the resized images, just use this:

// Cria thumbnail de uma imagem, com boa qualidade
//-----------------------------------------------
// William G. Comnisky
// William@waw.com.br

$ Imagem = "img.jpg ";

$ Img_origem = imagecreatefromjpeg ($ imagem );

$ Nova_largura = imagesx ($ img_origem)/4;
$ Nova_altura = imagesy ($ img_origem)/4;

$ Img_destino = imagecreatetruecolor ($ nova_largura, $ nova_altura );
Imagecopyresampled ($ img_destino,
$ Img_origem, 0, 0, 0, $ nova_largura, $ nova_altura, imagesx
($ Img_origem), imagesy ($ img_origem ));
ImageJPEG ($ img_destino );
?>



Rze@counter-strike.net
24-Jan-2002

"REDEFINE? BICUBIC? ARGH!

If you aren't happy with the quality of the resized images, just use this:

* Code *"

Bicubic is better than resampled. Both are not great looking, but bicubic
IS better looking.

In either case, resampled is only on GD 2.0. Most servers don't have 2.0
And I'm not even sure if bicubic works with 2.0.



Big_dog001@hotmail.com
03-Apr-2002

Regarding the bicubic function. I hand patched with all the notes above on
Php 4.1.2 on openbsd 2.8 with apache 1.3.12 and was getting errors in
Apache log file like:

Undefined symbol "_ GDLS_FETCH" called from [the libphp4.so]...

By removing the "GDLS_FETCH ()" in the suggested bicubic function
It started working on openbsd with no problems.

Not sure what this GDLS_FETCH function is but it wasn' t supported on
Openbsd ....

Hope this helps and thanks for a nice function.

Full text: www.php.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.