>백엔드 개발 >PHP 튜토리얼 >制作图片 size (jpg)_PHP

制作图片 size (jpg)_PHP

WBOY
WBOY원래의
2016-06-01 12:32:151158검색

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 dstH, 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 performed. 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 03:10

saw this one on the ng and it works fine - just trying to figure it out for
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,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));

ImageGif($dst_img);

?>



hoiarie@hotmail.com
15-Jul-2000 04:57

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



skurrilo@skurrilo.de
28-Nov-2000 07:36

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 could do that so it's included 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, dstH, 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);
dstH = Z_LVAL_PP(DH);
dstW = Z_LVAL_PP(DW);

// kopiera paletten

for( i=0; igdImageColorAllocate( im_dst, im_src->red[i], im_src->green[i],
im_src->blue[i] );
};

// gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH,
srcW, srcH);

scaleX = (float)(srcW-1) / (float)dstW;
scaleY = (float)(srcH-1) / (float)dstH;

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 06:33

re: imagecopyresizedbicubic

Yes, this function is really great! The thumbnails are much, much better.
However the example posted above is for PHP4, and if you need it for PHP3
(and you really should 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, dstH, 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;
dstH = 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; igdImageColorAllocate( 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)dstH;

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 06:49

One note on getting the ImageCopyResizedBicubic to work. Just adding the
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 the
"imagecopyresized" to "imagecopyresizedbicubic" in the
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
should be good to go. Scaling works much better with this function.



aleczapka@gmx.net
05-Apr-2001 09:35

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

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



darren@php4hosting.com
16-Jun-2001 01:10

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

Assuming you are using Linux and PHP-4.0.5 Source
Get the 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
Then do your normal configure/make/make install

Check PHPinfo under GD for ImageResizeBicubic enabled.



sfm15@columbia.edu
05-Jul-2001 11:37

I've been using the imagecopyresizedbicubic function and it works great at
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 26K while the original larger pictures are usually around 12K.
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 Jensen July 10 2001 -- original code (in C, for the
PHP GD Module) by jernberg@fairytale.se
*/
{
for ($i = 0; $i 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 $sY = $j * $scaleY;

for ($i = $src_x; $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 10:01

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

Something to remember when upgrading



info@methmedia.de
27-Sep-2001 01:12

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-Oct-2001 09:10

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



william@waw.com.br
12-Jan-2002 02:55

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,0,$nova_largura,$nova_altura,imagesx
($img_origem),imagesy($img_origem));
imageJPEG($img_destino);
?>



rze@counter-strike.net
24-Jan-2002 11:28

"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 09:30

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 the
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.

全文:www.php.net

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.