Что такое код imagecopyresized

Содержание

imagecopyresized

(PHP 4, PHP 5, PHP 7)

imagecopyresized � ����������� � ��������� ������� ����� �����������

��������

imagecopyresized() �������� ������������� ������� ������ ����������� �� ������ �����������. dst_image — �������������� �����������, src_image — ������������� ��������� �����������.

������� �������, imagecopyresized() ����� ������������� ������� �� src_image � ������� src_w � ������� src_h �� ����������� src_x , src_y � �������� ��� � ������������� ������� ����������� dst_image � ������� dst_w � ������� dst_h �� ����������� dst_x , dst_y .

���� ����������, ������ ��� ������ ��������� � ��������� ����������� ��������, ���������� �������� ����� �������� ��� ����. ���������� ������������� �� ������ �������� ���� �����������. ������� ����� ������������ ��� ��������� �������� �� �� �� �����������, � �������� ��� ����������� (���� dst_image ����� �� �� ��������, ��� � src_image ), �� ���� ������� ����� ������������, ��������� �������������.

������ ����������

������ �������� �����������.

������ ��������� �����������.

x-���������� ��������������� �����������.

y-���������� ��������������� �����������.

x-���������� ��������� �����������.

y-���������� ��������� �����������.

������ ��������� �����������.

������ ��������� �����������.

������������ ��������

���������� TRUE � ������ ��������� ���������� ��� FALSE � ������ ������������� ������.

�������

������ #1 ��������� ������� �����������

� ���� ������� ������ ����������� ����� �������� � ��� ����.

// ���� � ����� ������
$filename = ‘test.jpg’ ;
$percent = 0.5 ;

// ��� �����������
header ( ‘Content-Type: image/jpeg’ );

// ��������� ������ �������
list( $width , $height ) = getimagesize ( $filename );
$newwidth = $width * $percent ;
$newheight = $height * $percent ;

// ��������
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
$source = imagecreatefromjpeg ( $filename );

// ��������� �������
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );

// �����
imagejpeg ( $thumb );
?>

����������� ���������� ������� ������� ����� ���-�� ��������:

����������� ����� �������� � ����������� ��������. ���� ��������� �������� ����������� � ������ ��������, ����������� ������� imagecopyresampled() .

����������

���������� �������� ��������� � ������������� ���������� ����������� (255+1 ����). ��������������� ��� ���������� ����������� ������� ������ ������, ��� 255. ��� ������� ������ ������� � ��� ����� ����������� ��������� �����������. � ������ ���������� ����������� �� �������� ������� ����� ����, � ���� ��� �� �������, �� �������� ��������� (������������) ����������� ����. ��� �� ������ ��������� ��������� ����. ����� ������ ����� ������ � ���������� ������ (��� ��������� ������) �����������. ��� ���������� ���� ��������, ����������, ����������� truecolor-����������� � �������� ��������������, ����������� �������� imagecreatetruecolor() .

�������� �����

imagecopyresampled() — ����������� � ��������� ������� ����������� � ����������������

��� ����� ��� imagecopyresized

(PHP 3, PHP 4 , PHP 5)

imagecopyresized — Copy and resize part of an image

Description int imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

imagecopyresized() copies a rectangular portion of one image to another image. dst_image is the destination image, src_image is the source image >dst_image is the same as src_image ) but if the regions overlap the results will be unpredictable.

Замечание: There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor() .

Примеры

Пример 1. Resizing an image

This example will display the image at half size.

// File and new size
$filename = ‘test.jpg’ ;
$percent = 0.5 ;

// Content type
header ( ‘Content-type: image/jpeg’ );

// Get new sizes
list( $width , $height ) = getimagesize ( $filename );
$newwidth = $width * $percent ;
$newheight = $height * $percent ;

// Load
$thumb = imagecreate ( $newwidth , $newheight );
$source = imagecreatefromjpeg ( $filename );

// Resize
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );

// Output
imagejpeg ( $thumb );
?>

The image will be output at half size, though better quality could be obtained using imagecopyresampled() .

Смотрите также

This snippet allows you to grab a thumbnail from the center of a large image. This was used for a client photo gallery for art to give a teaser of the image to come (only a small portion). You could get dynamic with this value. I also put in a scaling factor in case you want to scale down first before chopping.

// The file
$filename = ‘moon.jpg’ ;
$percent = 1.0 ; // if you want to scale down first
$imagethumbsize = 200 ; // thumbnail size (area cropped in middle of image)
// Content type
header ( ‘Content-type: image/jpeg’ );

// Get new dimensions
list( $width , $height ) = getimagesize ( $filename );
$new_width = $width * $percent ;
$new_height = $height * $percent ;

// Resample
$image_p = imagecreatetruecolor ( $imagethumbsize , $imagethumbsize ); // true color for best quality
$image = imagecreatefromjpeg ( $filename );

imagejpeg ( $image_p , null , 100 );
?>

If you read your Imagedata from a Database Blob and use the functions from above to resize the image to a thumbnail improving a lot of traffic, you will have to make temporary copies of the files in order that the functions can access them

// switch through imagetypes
$extension = «jpg»;
if(mysql_result($query, 0, ‘type’) == «image/pjpeg»)
< $extension = "jpg"; >// overwrite
else if(mysql_result($query, 0, ‘type’) == «image/gif»)
< $extension = "gif"; >// overwrite

// get a temporary filename
// use microtime() to get a unique filename
// if you request more than one file f.e. by creating large numbers of thumbnails, the server could be not fast enough to save all these different files and you get duplicated copies and resizepics() will resize and output often the same content

// open
$tempfile = fopen($filename, «w+»);

// write
fwrite($tempfile, mysql_result($query, 0, ‘image’));

// resize and output the content
echo resizepics($filename, ‘100’, ’70’);

// delete temporary file
unlink($filename);

NOTE: this script has to be put into a file which sends correct header informations to the browser, otherwise you won’t get more to see than a big red cross :-)

Most of the examples below don’t keep the proportions properly. They keep using if/else for the height/width..and forgetting that you might have a max height AND a max width, not one or the other.

# taller
if ($height > $max_height) <
$w > $height = $max_height;
>

# wider
if ($width > $max_width) <
$height = ($max_width / $width) * $height;
$w > >

$image_p = imagecreatetruecolor($width, $height);

imagecopyresampled($image_p, $image, 0, 0, 0, 0,
$width, $height, $orig_width, $orig_height);

Resize image proportionaly where you give a max width or max height

( ‘Content-type: image/jpeg’ );
//$myimage = resizeImage(‘filename’, ‘newwidthmax’, ‘newheightmax’);
$myimage = resizeImage ( ‘test.jpg’ , ‘150’ , ‘120’ );
print $myimage ;

function resizeImage ( $filename , $newwidth , $newheight ) <
list( $width , $height ) = getimagesize ( $filename );
if( $width > $height && $newheight $height ) <
$newheight = $height / ( $width / $newwidth );
> else if ( $width $height && $newwidth $width ) <
$newwidth = $width / ( $height / $newheight );
> else <
$newwidth = $width ;
$newheight = $height ;
>
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
$source = imagecreatefromjpeg ( $filename );
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
return imagejpeg ( $thumb );
>
?>

If you need to delete or resize images in the filesystem (not in DB) without loosing the image quality.
I commented the code as much as possible so that newbies (like myself) will understand it. ;)

WRITTEN BY:
Finnur Eiriksson, (http://www.centrum.is/finnsi)
Based on snippets that have been posted on www.PHP.net
Drop me an e-mail if you have any questions.

NOTE:
This code is written to either delete or resize pictures in the file system, so if you have your pictures in a database
you will have to make some changes. Also, if you are using other picture formats than .gif’s or .jpg’s you
will have to add som code as well (Read the comments to find out where to do this).

IMPORTANT:
The $_GET[‘resizepic’] variable only contains the NAME of the file that is going to be deleted/resized.

The internet guest account (IUSR_SERVERNAME on WINDOWS) must have read and write permissions (execution permission not needed)
in your image directory (i.e. $dir_name = «FooBar»). It is a good idea to have a separate directory for images that users
can upload to and manipulate the contents. Ideally, you should have one directory for the pictures that are used for the website,
and another upload directory

$dir_name = «FooBar» ; // Enter the name of the directory that contains the images
$olddir = getcwd (); // Get the Current Windows Directory to be able to switch back in the end of the script
$dir = opendir ( $dir_name ); //make a directory handle
//To delete a picture
if(isset( $_GET [ ‘delpic’ ])) <
chdir ( ‘images’ );
$delpic = $_GET [ ‘delpic’ ];
@ unlink ( $delpic );
chdir ( $olddir );
>
//To resize a picture
if(isset( $_GET [ ‘resize’ ])) <
//$_GET[‘resize’] contains the resize-percentage (for example 80 and 40, for 80% and 40% respectively. To double the image in size the user enters 200 etc.)
// File and new size
$percent = ( $_GET [ ‘resize’ ]/ 100 );
chdir ( ‘images’ ); // change the windows directory to the image directory
$filename = $_GET [ ‘resizepic’ ];

// Decide the content type, NB:This code is written to only execute on .gif’s and .jpg’s
// If you want other formats than .gif’s and .jpg’s add your code here, in the same manner:
$format = » ;
if( preg_match ( «/.jpg/i» , «$filename» )) <
$format = ‘image/jpeg’ ;
header ( ‘Content-type: image/jpeg’ );
>
if ( preg_match ( «/.gif/i» , «$filename» )) <
$format = ‘image/gif’ ;
header ( ‘Content-type: image/gif’ );
>
if( $format != » ) < //This is where the actual resize process begins.
// Get new sizes
list( $width , $height ) = getimagesize ( $filename );
$newwidth = $width * $percent ;
$newheight = $height * $percent ;

// Load the image
switch( $format ) <
case ‘image/jpeg’ :
$source = imagecreatefromjpeg ( $filename );
break;
case ‘image/gif’ ;
$source = imagecreatefromgif ( $filename );
break;
>
//Get the Image
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
//This must be set to false in order to be able to overwrite the black
//pixels in the background with transparent pixels. Otherwise the new
//pixels would just be applied on top of the black background.
imagealphablending ( $thumb , false );
//Make a temporary file handle
$source = @ imagecreatefromjpeg ( $filename );
// Resize
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
//Write the image to the destination file
@ imagejpeg ( $thumb , $filename );
//Change back to the old directory. I’m not sure that this is neccessary
chdir ( $olddir );
//Specify where you want the user to go after the operation:
header ( ‘Location: foobar.php’ );
>
>

Илон Маск рекомендует:  Хороший вопрос Как поставить степень в Word

Just in addition to the script posted by marcy DOT xxx (AT) gmail.com:

There is one error in this script, that’s why it didn’t work for me.

Instead of this line:
$source = @function_image_create($imgfile);

use this line:
$source = @$function_image_create($imgfile);

This example allow to use every kind of image and to resize images with ImageCopyResized(), maintaining proportions..

// switch to find the correct type of function

$imgfile = ‘namefile.jpg’ ;
Header ( «Content-type: image/» . $_GET [ «type» ]);

switch( $_GET [ «type» ]) <
default:
$function_image_create = «ImageCreateFromJpeg» ;
$function_image_new = «ImageJpeg» ;
break;
case «jpg» :
$function_image_create = «ImageCreateFromJpeg» ;
$function_image_new = «ImageJpeg» ;
case «jpeg» :
$function_image_create = «ImageCreateFromJpeg» ;
$function_image_new = «ImageJpeg» ;
break;
case «png» :
$function_image_create = «ImageCreateFromPng» ;
$function_image_new = «ImagePNG» ;
break;
case «gif» :
$function_image_create = «ImageCreateFromGif» ;
$function_image_new = «ImagePNG» ;
break;
>

list( $width , $height ) = getimagesize ( $imgfile );

// the new weight of the thumb

// Proportion is maintained

$newwidth = (int) (( $width * 80 )/ $height );

$thumb = ImageCreateTrueColor ( $newwidth , $newheight );
$source = @ function_image_create ( $imgfile );

ImageCopyResized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );

Neat script to create a thumbnails no larger than 150 (or user-specific) height AND width.

= «» # picture fileNAME here. not address
$max = 150 ; # maximum size of 1 side of the picture.
/*
here you can insert any specific «if-else»,
or «switch» type of detector of what type of picture this is.
in this example i’ll use standard JPG
*/

$src_img = ImagecreateFromJpeg ( $picture );

$oh = imagesy ( $src_img ); # original height
$ow = imagesx ( $src_img ); # original width

$new_h = $oh ;
$new_w = $ow ;

if( $oh > $max || $ow > $max ) <
$r = $oh / $ow ;
$new_h = ( $oh > $ow ) ? $max : $max * $r ;
$new_w = $new_h / $r ;
>
// note TrueColor does 256 and not.. 8
$dst_img = ImageCreateTrueColor ( $new_w , $new_h );

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

����� ������� ��� ������������� ��� �������� ������� imagecopyresampled () ��� imagecopyresized ()?

� ����� ���������������� �������� ����������� �� ���� �����, ������� ���� ������������� ����������� ��������� ����������� ������ ������� �� ����. ��� ������� �������� ������������� ���������� �� ������� ��������. � ���������� ������ ���������� ����, � � ���� ��������� � ������� ������� imagecopyresampled (), ����� �������� ������ � ��������� �� ��� imagecopyresized (). ����� �� ��� ����� ������������? ������ ��������?

imagecopyresized () �������� ������������� ����� ������ ����������� � ������� �����������. dst_image ����� ����������� ����������, src_image �������� ��������������� ��������� �����������.

imagecopyresampled () �������� ������������� ����� ������ ����������� � ������� �����������, ������ ���������������� �������� ��������, ��� ���, � ���������, ���������� ������� ����������� ��-�������� ��������� ������� �������.

php.net �������� ����� ������

��� �������� ������� ����������. ������ �� ���. ����� � ������ ��������� imagecopyresampled ()

PHP imagecopyresized ������ imagecopyresampled ������ imagecopy ����� / ������

November 2020

27.6k ���

��� ���, �������, ������ �� �� �����. ������ ����� / ������ ������� �� ���.

imagecopyresized () ������ imagecopyresampled () ������ imagecopy ().

� ��������� �������, �������������� ������������� �����������.

����� �������, � ���� ���� �������� �����������, ��������� � ‘$ newImage = imagecreatetruecolor (250, 250).

� ������ � ����, ����� ����������� �������� ����������� � �$ newImage�

1 ������

imagecopyresized ����� ���������� � ������� � �����������. ��� ���� ������������ �������� ����������� �������� , ������� ��������� �������� ����� ���������� ����������.

imagecopyresampled ����� ���������� � ������� � �����������, �� ���������� ����������� � ���������� ���������������� �������� , ������� ������ ���� ������� ������ ���������� , �� imagecopyresized �� ���� ���������� ������������� ������������ ����������.

imagecopy ����� ���������� , �� �� ����� �������������� �����������.

������ ������� �imagecopyresized� � ������� �imagecopyresampled� � ������ �������� �����������

� ��������� ����� � ������� �� ���-����� Pligg CMS � ��� ������ �������� ����������� �� ���������, ������� ����������� ��������� � ���������� �� ��������������� ������������� ������, ��������� ���������� GD ��� ��������� �����������. ��������� ������� � �������� ��������, � ����� ���������� ������ � ��������� � ���������, ��� � ������ �������� ������� imagecopyresized � ������� imagecopyresampled .

������� �������� � ���, ��� � ������� � ���-����������, � � �� ���� � ���� ������. ���� ���� � �����, ��� (��������, �����������) �������� �� ��������� ����������� � ��������� � ��������������:

�� ����, ��� � ����, ����������� ������� �������������� � ������� ������� imagecreatruecolor � ����� tmp_img , ������� ����� �������������� � ������� ������� imagecopyresized .

��������� � ���� ��� �����, � �� ���� �������, �������� �� ��� ���������� ����� ��� ��������� ������� ������� XY ��� �������� ��� ��������. ������ �� � �������� imagecreatetruecolor � imagecopyresized �� imagecopyresampled ?

php imagecopyresized vs imagecopyresampled vs imagecopy pros/cons

These all seem to do the same thing. What are the pros/cons of each.

imagecopyresized() vs imagecopyresampled() vs imagecopy().

I’m resizing a user submitted image.

So I have an image shell created with ‘$newImage=imagecreatetruecolor(250, 250)’.

And now I want to copy the orginal image into the ‘$newImage’

1 Answer 1

imagecopyresized will copy and scale and image. This uses a fairly primitive algorithm that tends to yield more pixelated results.

imagecopyresampled will copy and scale and image, it uses a smoothing and pixel interpolating algorithm that will generally yield much better results then imagecopyresized at the cost of a little cpu usage.

imagecopy will copy but will not scale the image.

ResizeImageGet

����� ��������� �������� � ��������� ����������� ����� � ����� /upload/resize_cache/����. ���� ��� �������� ����������� �������� ���������� ����, ������� ��������� ��� ����������� ���������� �� ��������� �������� �� ���������� �����������. ��� ��������� ������ ����� ������ ���� � ������������ �����. ����������� �����.

���������

�������� �������� � ������
file ������������� ����� �� ������� b_file ��� ������ �������� ����� (Array(FILE_NAME, SUBDIR, WIDTH, HEIGHT, CONTENT_TYPE)), ���������� ������� GetFileArray.
Size ������ � ���� Array(«width»=>WIDTH, «height»=>HEIGHT) �� ���������� ������ � ������ ��� ����������� ��������. ��� ����� �����������.
resizeType ��� ���������������:
  • BX_RESIZE_IMAGE_EXACT — ������������ � ������������� $arSize c ����������� ���������, ������� ������;
  • BX_RESIZE_IMAGE_PROPORTIONAL — ������������ � ����������� ���������, ������ �������������� $arSize;
  • BX_RESIZE_IMAGE_PROPORTIONAL_ALT — ������������ � ����������� ��������� �� ������ ��� ���� ����������� ������������ �������� �� ������/������, ������ �������������� $arSize, ���������� ��������� ������������ ��������.
InitSizes ���� ����������� � �������������� ������ �������� ���������� ��������. True — ����������, false — ���
Filters ������ �������� ��� ������������� �������� � ������� ��������: array(array(«name» => «sharpen», «precision» => 15)). �������� ���� ���� — sharpen. �������� ��� �� ����������� — ����� ��������������� ���������. ������������ ��� ��������� �������� � ��������. 10.0.4
Immediate ���� ���������� � ���������� ������� OnBeforeResizeImage, �� ������ �������� ��������������� ��������������� ��� ������ ������. ���������� � �������� ����� ��������� ���������� ���������������. 12.0.10
jpgQuality �����, ��������������� � ��������� �������� JPG ��� ���������������. ��� ������ ��������, ��� ���� �������� � ������ ������ ����� �����������. 12.5.0

������������ ��������

����� ���������� ������ ����:

������� �������������

�������� ������� ���� ����� ����� �������:

���������������� �����������

�� ����� ����, ���� ������������ ������� ���� ����������� �� ������������� ������������� ������� �������.

��� ����� ����� ����� ���� �������������� �� �����

�� �������, ��� ���������������� �����������, �������� �� ���������, �� �������� ����������� �������������. ��������������� �� �� ������������� ����� ��� ������������.

����� ���������������� ����������� �� �������� ������ ��� ���������� �����������. �� �������� �������� ����������� �� ������.

���������, ��� ������������� ���������������� ������ sharpen �������� ��������� �� PNG � �������������. ����� �������� �����, ����� ������ precision ������ ����

ResizeImageGet: ������� �������� ������ (������) ����������� � Bitrix

������ � ��� ����������� ����������� ���������, ��������� � ����� ���������� ��� ���� � ������ ���� �������� � �����, �������, � ���� �������, �������� ��� ����������� ��������� �������� �������� �����.

�������� ������ �� ��� ������������� � CMS WordPress, � ������ ������� ����������� ����������� � � Bitrix.

����, � Bitrix ���������� ����� �ResizeImageGet�, � ������� �������� � ����� ����������� ������ �����������. ������ ������������� ���������:

1. ������� �������� � �������� �� ��������� �� ����� (������ ������ �������� ����������� � ��������), ��������, �300� � �450� ��������������.

2.BX_RESIZE_IMAGE_EXACT� � ���� �� ���������� ��������������� �����������. ���������� ��������:

  1. BX_RESIZE_IMAGE_EXACT � �������� ������� � ����������� ��������� ��� ��������� ��������� � ���������� �������.
  2. BX_RESIZE_IMAGE_PROPORTIONAL � �������� ������� � ����������� ���������. ������ �������������� ���������� �����������.
  3. BX_RESIZE_IMAGE_PROPORTIONAL_ALT � �������� ������� � ����������� ���������, �� ������ ��� ���� ������� ������������ �������� �� ������/������. ������ ����� �������������� ���������� �����������. ������� �������� ��� ��������� ������������ ��������.

3.false� � ���������� �� ������ � ������� � �������� ����������� �����������. ���������� ��������:

��� �� ����������� � ������ ����� ����������. �������� ��������, ����� ����� �$arItem[«PREVIEW_PICTURE»]� ���������� ��� ������ �� �����������. ���������� ���� ������ ����� �var_dump�.

��� ������ ����������� ��� ����� ����������� ����� ����������� �������������, � ��������� ��� ����� � ����� /upload/resize_images/.

Imagecopyresized

Php �������


Php �������


imagecopyresized

(PHP 3, PHP 4, PHP 5)

imagecopyresized — Copy and resize part of an image

Description

bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

imagecopyresized() copies a rectangular portion of one image to another image. dst_image is the destination image, src_image is the source image > If the source and destination coordinates and w >copy regions within the same image ( if dst_image is the same as src_image ) but if the regions overlap the results will be unpredictable.

���������: There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor().

�������

������ 1. Resizing an image

Илон Маск рекомендует:  Моделирование при сжатии текстовых данных lzr

This example will display the image at half size.

// File and new size
$filename = ‘test.jpg’ ;
$percent = 0.5 ;

// Content type
header ( ‘Content-type: image/jpeg’ );

// Get new sizes
list ( $width , $height ) = getimagesize ( $filename );
$newwidth = $width * $percent ;
$newheight = $height * $percent ;

// Load
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
$source = imagecreatefromjpeg ( $filename );

// Resize
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );

// Output
imagejpeg ( $thumb );
?>

The image will be output at half size, though better quality could be obtained using imagecopyresampled().

�������� �����


User Contributed Notes

konteineris at yahoo dot com
16-Feb-2006 05:42

Function creates a thumbnail from the source image, resizes it so that it fits the desired thumb width and height or fills it grabbing maximum image part and resizing it, and lastly writes it to destination

function thumb ( $filename , $destination , $th_width , $th_height , $forcefill )
<
list ( $width , $height ) = getimagesize ( $filename );

$source = imagecreatefromjpeg ( $filename );

if ( $width > $th_width || $height > $th_height ) <
$a = $th_width / $th_height ;
$b = $width / $height ;

if (( $a > $b )? $forcefill )
<
$src_rect_width = $a * $height ;
$src_rect_height = $height ;
if (! $forcefill )
<
$src_rect_width = $width ;
$th_width = $th_height / $height * $width ;
>
>
else
<
$src_rect_height = $width / $a ;
$src_rect_width = $width ;
if (! $forcefill )
<
$src_rect_height = $height ;
$th_height = $th_width / $width * $height ;
>
>

$src_rect_xoffset = ( $width — $src_rect_width )/ 2 * intval ( $forcefill );
$src_rect_yoffset = ( $height — $src_rect_height )/ 2 * intval ( $forcefill );

$thumb = imagecreatetruecolor ( $th_width , $th_height );
imagecopyresized ( $thumb , $source , 0 , 0 , $src_rect_xoffset , $src_rect_yoffset , $th_width , $th_height , $src_rect_width , $src_rect_height );

imagejpeg ( $thumb , $destination );
>
>

?>

jesse at method studios
04-Oct-2005 11:07

imagecopyresized() does not do any resampling. This makes it extremely quick. If quality is an issue, use imagecopyresampled().
MaLaZ
12-Aug-2005 06:24

simple script for creating thumbnails with process information and saving original ratio thumbnail to new destination. good then useing with upload or uploaded images:

//Upload————————————
if (isset( $submit ))
<
if ( $_FILES [ ‘imagefile’ ] [ ‘type’ ] == «image/jpeg» ) <
copy ( $_FILES [ ‘imagefile’ ] [ ‘tmp_name’ ] , «../images/» . $_FILES [ ‘imagefile’ ] [ ‘name’ ] )
or die ( «Could not copy» );
echo «» ;
echo «Image Name: » . $_FILES [ ‘imagefile’ ] [ ‘name’ ] . «» ;
echo «
Image Size: » . $_FILES [ ‘imagefile’ ] [ ‘size’ ] . «» ;
echo «
Image Type: » . $_FILES [ ‘imagefile’ ] [ ‘type’ ] . «» ;
echo «
Image Copy Done.
» ;
>
else <
echo «

» ;
echo «bad file type (» . $_FILES [ ‘imagefile’ ] [ ‘name’ ] . «)
» ;exit;
>
//——upload end

$thumb size = 120 ;
echo «Thumbnail Info:

1.Thumb defined size: — OK: $thumb size
» ;
$im gfile = «../images/ $im age file_name » ; //processed image
echo »
2.Image destination: — OK: $im gfile
» ;
header ( ‘Content-type: image/jpeg’ );
list ( $width , $height ) = getimagesize ( $im gfile );
echo «3.Image size — OK: W= $width x H= $height
» ;
$im gratio = $width / $height ;
echo «3.Image ratio — OK: $im gratio
» ;
if ( $im gratio > 1 ) <
$newwidth = $thumb size ;
$newheight = $thumb size / $im gratio ; >
else <
$newheight = $thumb size ;
$newwidth = $thumb size * $im gratio ; >
echo «4.Thumb new size -OK: W= $newwidth x H= $newheight
» ;
$thumb = ImageCreateTrueColor ( $newwidth , $newheight );
echo «5.TrueColor — OK
» ;
$source = imagecreatefromjpeg ( $im gfile );
echo «6.From JPG — OK
» ;
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
imagejpeg ( $thumb , «../images/thumbs/thumb_ $im age file_name » , 100 );echo «7.Done. — OK
» ;
//————end—————
?>

or without any info, just resizing:

//——start thumbnailer
$thumb size = 120 ;
$im gfile = «../images/ $im age file_name » ;
header ( ‘Content-type: image/jpeg’ );
list ( $width , $height ) = getimagesize ( $im gfile );
$im gratio = $width / $height ;
if ( $im gratio > 1 ) <
$newwidth = $thumb size ;
$newheight = $thumb size / $im gratio ; >
else <
$newheight = $thumb size ;
$newwidth = $thumb size * $im gratio ; >
$thumb = ImageCreateTrueColor ( $newwidth , $newheight );
$source = imagecreatefromjpeg ( $im gfile );
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
imagejpeg ( $thumb , «../images/thumbs/thumb_ $im age file_name » , 100 );
//————end—————
?>

i hope it helps.
FearINC at gmail dot com
10-Aug-2005 09:35

I wrote a function not long ago that creates a thumbnail out of a large picture. Unlike other notes on this page, the code is a function so it can be used many times on the same script. The function allows the programer to set max height and width and resizes the picture proportionally.

function saveThumbnail ( $save ToDir , $im age Path , $im age Name , $max _x , $max _y ) <
preg_match ( «‘?(.*)\.(gif|jpe?g|png)$’i» , $im age Name , $ext );
switch ( strtolower ( $ext [ 2 ] )) <
case ‘jpg’ :
case ‘jpeg’ : $im = imagecreatefromjpeg ( $im age Path );
break;
case ‘gif’ : $im = imagecreatefromgif ( $im age Path );
break;
case ‘png’ : $im = imagecreatefrompng ( $im age Path );
break;
default : $stop = true;
break;
>

if (( $max _x / $max _y ) $x / $y )) <
$save = imagecreatetruecolor ( $x /( $x / $max _x ), $y /( $x / $max _x ));
>
else <
$save = imagecreatetruecolor ( $x /( $y / $max _y ), $y /( $y / $max _y ));
>
imagecopyresized ( $save , $im , 0 , 0 , 0 , 0 , imagesx ( $save ), imagesy ( $save ), $x , $y );

imagegif ( $save , » < $save ToDir > < $ext [ 1 ] >.gif» );
imagedestroy ( $im );
imagedestroy ( $save );
>
>
?>

the function for now takes only jpg,gif and png files, but that can easily be changed.
It’s an easy to use easy to understand function and I hope it will come useful to someone.
mecdesign at hotmail dot com
08-Aug-2005 01:51

This code will resize your images if your image needs to fit (without stretching) into a max height / width destination image that is not a 1:1 ratio (mine was 4:3).

// Ratios
$im age _ratio = $width / $height ; // Actual image’s ratio
$destination _ratio = $max _width / $max _height ; // Ratio of dest. placeholder

// Taller
if ( $im age _ratio $destination _ratio )
<
//Too tall:
if ( $height > $max _height )
<
$height = $max _height ;
$width = ceil ( $height / $im age _ratio );
>
>
// Wider / balanced & too wide:
else if ( $width > $max _width )
<
$width = $max _width ;
$height = ceil ( $width / $im age _ratio );
>
?>

saurabh at saurabh dot com
22-Jul-2005 03:57

I am trying to make thumbnails of image(png, jpg, gif).

Not a single code is working. Do we have to make any changes in php.ini?

which version of gd is required can anybody xplain me whole thing.
brian tyler gmail com
12-Jun-2005 02:53

I spent last night getting unsuccessful results from this function until I found this note of fluffle gmail on the imagecopyresampled page, I have made a slight modification, so you can just cut and paste.

Just to clarify that src_w and src_h do not necessarily need to be the source image width and height, as they specify the size of the rectangle cropped from the source picture, with its top left corner situated at (src_x, src_y).

For example, the code below crops a jpeg image to be square, with the square situated in the centre of the original image, and then resizes it to a 100×100 thumbnail.

function ( $im age _filename , $thumb _location , $im age _thumb _size ) <
//@ $im age _filename — the filename of the image you want
//to get a thumbnail for (relative to location of this
//function).
//@ $thumb _location — the url (relative to location of this
//function) to save the thumbnail.
//@ $im age _thumb _size — the x-y dimension of your thumb
//in pixels.

list ( $ow , $oh ) = getimagesize ( $im age _filename );
$im age _original = imagecreatefromjpeg( $im age _filename );
$im age _thumb = imagecreatetruecolor( $im age _thumb _size , $im age _thumb _size );
if ( $ow > $oh ) <
$off_w = ( $ow — $oh )/2;
$off_h = 0;
$ow = $oh ;
> elseif ( $oh > $ow ) <
$off_w = 0;
$off_h = ( $oh — $ow )/2;
$oh = $ow ;
> else <
$off_w = 0;
$off_h = 0;
>
imagecopyresampled( $im age _thumb , $im age _original , 0, 0, $off_w , $off_h , 100, 100, $ow , $oh );

imagejpeg ( $im age _thumb , $thumb _location );
> //end function
no at name dot com
18-May-2005 03:45

I was searching for script, that do not resize on the fly, but copy resized file to other place, so after long searches, i’ve done this all.php?act=funct&argument= I hope it will be usefull for someone:
(This is not original code, i’v taked it from somwhere and edited a ltl :)

function resize ( $cur_dir , $cur_file , $newwidth , $output_dir )
<
$dir _name = $cur_dir ;
$olddir = getcwd ();
$dir = opendir ( $dir _name );
$filename = $cur_file ;
$format = » ;
if ( preg_match ( «/.jpg/i» , » $filename » ))
<
$format = ‘image/jpeg’ ;
>
if ( preg_match ( «/.gif/i» , » $filename » ))
<
$format = ‘image/gif’ ;
>
if ( preg_match ( «/.png/i» , » $filename » ))
<
$format = ‘image/png’ ;
>
if ( $format != » )
<
list ( $width , $height ) = getimagesize ( $filename );
$newheight = $height * $newwidth / $width ;
switch ( $format )
<
case ‘image/jpeg’ :
$source = imagecreatefromjpeg ( $filename );
break;
case ‘image/gif’ ;
$source = imagecreatefromgif ( $filename );
break;
case ‘image/png’ :
$source = imagecreatefrompng ( $filename );
break;
>
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
imagealphablending ( $thumb , false );
$source = @imagecreatefromjpeg ( » $filename » );
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
$filename = » $output_dir / $filename » ;
@imagejpeg ( $thumb , $filename );
>
>
?>

call this function using

resize ( «./input folder» , «picture_file_name» , «width» , «./output folder» );
?>

del at kartoon dot net
05-May-2005 12:37

This snippet allows you to grab a thumbnail from the center of a large image. This was used for a client photo gallery for art to give a teaser of the image to come (only a small portion). You could get dynamic with this value. I also put in a scaling factor in case you want to scale down first before chopping.

// The file
$filename = ‘moon.jpg’ ;
$percent = 1.0 ; // if you want to scale down first
$im age thumbsize = 200 ; // thumbnail size (area cropped in middle of image)
// Content type
header ( ‘Content-type: image/jpeg’ );

// Get new dimensions
list ( $width , $height ) = getimagesize ( $filename );
$new_w idth = $width * $percent ;
$new_h eight = $height * $percent ;

// Resample
$im age _p = imagecreatetruecolor ( $im age thumbsize , $im age thumbsize ); // true color for best quality
$im age = imagecreatefromjpeg ( $filename );

imagejpeg ( $im age _p , null , 100 );
?>

development at lab-9 dot com
26-Apr-2005 02:35

If you read your Imagedata from a Database Blob and use the functions from above to resize the image to a thumbnail improving a lot of traffic, you will have to make temporary copies of the files in order that the functions can access them

// switch through imagetypes
$ext ension = «jpg»;
if (mysql_result( $query , 0, ‘type’) == «image/pjpeg»)
< $ext ension = "jpg"; >// overwrite
else if(mysql_result( $query , 0, ‘type’) == «image/gif»)
< $ext ension = "gif"; >// overwrite

// get a temporary filename
// use microtime () to get a unique filename
// if you request more than one file f.e. by creating large numbers of thumbnails, the server could be not fast enough to save all these different files and you get duplicated copies and resizepics() will resize and output often the same content

$filename = microtime ().»_temp.». $ext ension ;

// open
$tempfile = fopen ( $filename , «w+»);

// write
fwrite ( $tempfile , mysql_result( $query , 0, ‘image’));

// close
fclose ( $tempfile );

// resize and output the content
echo resizepics( $filename , ‘100’, ’70’);

// delete temporary file
unlink ( $filename );

NOTE: this script has to be put into a file which sends correct header informations to the browser, otherwise you won’t get more to see than a big red cross :-)
robby at planetargon dot com
28-Feb-2005 06:56

Most of the examples below don’t keep the proportions properly. They keep using if /else for the height/w >max height AND a max width, not one or the other.

$width = $orig_width ;
$height = $orig_height ;

# taller
if ( $height > $max _height ) <
$width = ( $max _height / $height ) * $width ;
$height = $max _height ;
>

# wider
if ( $width > $max _width ) <
$height = ( $max _width / $width ) * $height ;
$width = $max _width ;
>

$im age _p = imagecreatetruecolor( $width , $height );

$im age = imagecreatefromjpeg( $filename );

imagecopyresampled( $im age _p , $im age , 0, 0, 0, 0,
$width , $height , $orig_width , $orig_height );

return $im age _p ;
>
haker4o at haker4o dot org
27-Feb-2005 07:57

// Resize image.
// Writeen By: Smelban & Haker4o
// Mails smelban@smwebdesigns.com & Haker4o@Haker4o.org
// This code is written to only execute on jpg,gif,png
// $picname = resizepics(‘pics’, ‘ new w >new heightmax’);
// Demo $picname = resizepics(‘stihche.jpg’, ‘180’, ‘140’);
$picname = resizepics ( ‘picture-name.format’ , ‘180’ , ‘140’ );
echo $pickname ;
//Error
die ( » File not exists :( » );
//Funcion resizepics
function resizepics ( $pics , $newwidth , $newheight ) <
if ( preg_match ( «/.jpg/i» , » $pics » )) <
header ( ‘Content-type: image/jpeg’ );
>
if ( preg_match ( «/.gif/i» , » $pics » )) <
header ( ‘Content-type: image/gif’ );
>
list ( $width , $height ) = getimagesize ( $pics );
if ( $width > $height && $newheight $height ) <
$newheight = $height / ( $width / $newwidth );
> else if ( $width $height && $newwidth $width ) <
$newwidth = $width / ( $height / $newheight );
> else <
$newwidth = $width ;
$newheight = $height ;
>
if ( preg_match ( «/.jpg/i» , » $pics » )) <
$source = imagecreatefromjpeg ( $pics );
>
if ( preg_match ( «/.jpeg/i» , » $pics » )) <
$source = imagecreatefromjpeg ( $pics );
>
if ( preg_match ( «/.jpeg/i» , » $pics » )) <
$source = Imagecreatefromjpeg ( $pics );
>
if ( preg_match ( «/.png/i» , » $pics » )) <
$source = imagecreatefrompng ( $pics );
>
if ( preg_match ( «/.gif/i» , » $pics » )) <
$source = imagecreatefromgif ( $pics );
>
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
return imagejpeg ( $thumb );
if ( preg_match ( «/.jpg/i» , » $pics » )) <
return imagejpeg ( $thumb );
>
if ( preg_match ( «/.jpeg/i» , » $pics » )) <
return imagejpeg ( $thumb );
>
if ( preg_match ( «/.jpeg/i» , » $pics » )) <
return imagejpeg ( $thumb );
>
if ( preg_match ( «/.png/i» , » $pics » )) <
return imagepng ( $thumb );
>
if ( preg_match ( «/.gif/i» , » $pics » )) <
return imagegif ( $thumb );
>
>
?>

smelban at smwebdesigns dot com
15-Feb-2005 07:37

Resize image proportionaly where you give a max w >max height

header ( ‘Content-type: image/jpeg’ );
// $myimage = resizeImage(‘filename’, ‘newwidthmax’, ‘newheightmax’);
$myimage = resizeImage ( ‘test.jpg’ , ‘150’ , ‘120’ );
print $myimage ;

function resizeImage ( $filename , $newwidth , $newheight ) <
list ( $width , $height ) = getimagesize ( $filename );
if ( $width > $height && $newheight $height ) <
$newheight = $height / ( $width / $newwidth );
> else if ( $width $height && $newwidth $width ) <
$newwidth = $width / ( $height / $newheight );
> else <
$newwidth = $width ;
$newheight = $height ;
>
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
$source = imagecreatefromjpeg ( $filename );
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
return imagejpeg ( $thumb );
>
?>

finnsi at centrum dot is
13-Feb-2005 04:30

If you need to delete or resize images in the filesystem (not in DB) without loosing the image quality.
I commented the code as much as possible so that newbies (like myself) will understand it. ;)

WRITTEN BY:
Finnur Eiriksson, ( )
Based on snippets that have been posted on www.PHP.net
Drop me an e-mail if you have any questions.

NOTE:
This code is written to either delete or resize pictures in the file system, so if you have your pictures in a database
you will have to make some changes. Also, if you are using other picture formats than .gif’s or .jpg’s you
will have to add som code as well (Read the comments to find out where to do this).

IMPORTANT:
The $_GET [ ‘resizepic’ ] variable only contains the NAME of the file that is going to be deleted/resized.

The internet guest account (IUSR_SERVERNAME on WINDOWS) must have read and write permissions (execution permission not needed)
in your image directory (i.e. $dir _name = «FooBar»). It is a good >for images that users
can upload to and manipulate the contents. >for the pictures that are used for the website,
and another upload directory

$dir _name = «FooBar» ; // Enter the name of the directory that contains the images
$olddir = getcwd (); // Get the Current Windows Directory to be able to switch back in the end of the script
$dir = opendir ( $dir _name ); //make a directory handle
//To delete a picture
if (isset( $_GET [ ‘delpic’ ] )) <
chdir ( ‘images’ );
$delpic = $_GET [ ‘delpic’ ] ;
@unlink ( $delpic );
chdir ( $olddir );
>
//To resize a picture
if (isset( $_GET [ ‘resize’ ] )) <
// $_GET [ ‘resize’ ] contains the resize-percentage ( for example 80 and 40, for 80% and 40% respectively. To double the image in size the user enters 200 etc.)
// File and new size
$percent = ( $_GET [ ‘resize’ ] / 100 );
chdir ( ‘images’ ); // change the windows directory to the image directory
$filename = $_GET [ ‘resizepic’ ] ;

// Decide the content type, NB:This code is written to only execute on .gif’s and .jpg’s
// If you want other formats than .gif’s and .jpg’s add your code here, in the same manner:
$format = » ;
if ( preg_match ( «/.jpg/i» , » $filename » )) <
$format = ‘image/jpeg’ ;
header ( ‘Content-type: image/jpeg’ );
>
if ( preg_match ( «/.gif/i» , » $filename » )) <
$format = ‘image/gif’ ;
header ( ‘Content-type: image/gif’ );
>
if ( $format != » ) < //This is where the actual resize process begins.
// Get new sizes
list ( $width , $height ) = getimagesize ( $filename );
$newwidth = $width * $percent ;
$newheight = $height * $percent ;

// Load the image
switch ( $format ) <
case ‘image/jpeg’ :
$source = imagecreatefromjpeg ( $filename );
break;
case ‘image/gif’ ;
$source = imagecreatefromgif ( $filename );
break;
>
//Get the Image
$thumb = imagecreatetruecolor ( $newwidth , $newheight );
//This must be set to false in order to be able to overwrite the black
//pixels in the background with transparent pixels. Otherwise the new
//pixels would just be applied on top of the black background.
imagealphablending ( $thumb , false );
//Make a temporary file handle
$source = @imagecreatefromjpeg ( $filename );
// Resize
imagecopyresized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );
//Write the image to the destination file
@imagejpeg ( $thumb , $filename );
//Change back to the old directory. I’m not sure that this is neccessary
chdir ( $olddir );
//Specify where you want the user to go after the operation:
header ( ‘Location: foobar’ );
>
>

?>

thomas at dermueller dot com
11-Feb-2005 12:23

Just in addition to the script posted by marcy DOT xxx (AT) gmail.com:

There is one error in this script, that’s why it d >for me.

Instead of this line:
$source = @function_image_create( $im gfile );

use this line:
$source = @ $function_image_create ( $im gfile );
marcy DOT xxx (AT) gmail.com
02-Jan-2005 10:06

This example allow to use every kind of image and to resize images with ImageCopyResized(), maintaining proportions..

// switch to find the correct type of function

$im gfile = ‘namefile.jpg’ ;
Header ( «Content-type: image/» . $_GET [ «type» ] );

switch ( $_GET [ «type» ] ) <
default:
$function_image_create = «ImageCreateFromJpeg» ;
$function_image_new = «ImageJpeg» ;
break;
case «jpg» :
$function_image_create = «ImageCreateFromJpeg» ;
$function_image_new = «ImageJpeg» ;
case «jpeg» :
$function_image_create = «ImageCreateFromJpeg» ;
$function_image_new = «ImageJpeg» ;
break;
case «png» :
$function_image_create = «ImageCreateFromPng» ;
$function_image_new = «ImagePNG» ;
break;
case «gif» :
$function_image_create = «ImageCreateFromGif» ;
$function_image_new = «ImagePNG» ;
break;
>

list ( $width , $height ) = getimagesize ( $im gfile );

// the new weight of the thumb

// Proportion is maintained

$newwidth = ( int ) (( $width * 80 )/ $height );

$thumb = ImageCreateTrueColor ( $newwidth , $newheight );
$source = @function_image_create ( $im gfile );

ImageCopyResized ( $thumb , $source , 0 , 0 , 0 , 0 , $newwidth , $newheight , $width , $height );

@ $function_image_new ( $thumb );
?>

backglancer at hotmail
14-Dec-2004 06:10

Neat script to create a thumbnails no larger than 150 (or user-specific) height AND width.

$picture = «» # picture fileNAME here. not address
$max = 150 ; # maximum size of 1 side of the picture.
/*
here you can insert any specific «if-else»,
or «switch» type of detector of what type of picture this is.
in this example i’ll use standard JPG
*/

$src_img = ImagecreateFromJpeg ( $picture );

$oh = imagesy ( $src_img ); # original height
$ow = imagesx ( $src_img ); # original width

$new_h = $oh ;
$new_w = $ow ;

if ( $oh > $max || $ow > $max ) <
$r = $oh / $ow ;
$new_h = ( $oh > $ow ) ? $max : $max * $r ;
$new_w = $new_h / $r ;
>
// note TrueColor does 256 and not.. 8
$dst_img = ImageCreateTrueColor ( $new_w , $new_h );

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

ImageJpeg ( $dst_img , «th_ $picture » );

?>

skurrilo at skurrilo dot de
28-Nov-2000 06:36

If you aren’t happy with the quality of the resized images, just give ImageMagick a try. ( ) This is a commandline tool for converting and viewing images.

��� ����� ��� imagecopyresized

imagecopyresized — �������� � �������� ������� ����� �����������.

��������

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() �������� ������������� ����� ����������� � ������ �����������, ������ ������������ ��������� �������� ����� �������, ���, � ���������, ���������� ������� ����������� �������� ��� �������� � �������. Dst_im ��� ����������� ����������, src_im — �������� �����������. ���� ���������� � ������ � ������ ��������� � ���������� ��������, ����� ��������� ��������������� ������������ � ������ �����������. ���������� ���� ������������ �������� ������ ����. ��� ������� ����� �������������� ��� ����������� �������� � �������� ������ ����������� (���� dst_im — �� ��, ��� � src_im ), �� ���� ������� �������������, ��������� �������������.

Илон Маск рекомендует:  Можно ли отключить определенный элемент в radiogroup
Понравилась статья? Поделиться с друзьями:
Кодинг, CSS и SQL