|
 |
imagecopyresized (PHP 3, PHP 4, PHP 5) imagecopyresized -- Copy and resize part of an image Descriptionbool 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 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_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.
<?php
$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);
?>
|
The image will be output at half size, though better
quality could be obtained using imagecopyresampled().
|
imagecopyresized
06madsenl at westseneca dot wnyric dot org
21-Apr-2006 06:05
I was fiddling with this a few days trying to figure out how to resize the original images on my website, but this site:
http://www.sitepoint.com/article/image-resizing-php
Has a great tutorial on using PHP to resize images without creating thumbnails. It was exactly what I was looking to do.
konteineris at yahoo dot com
15-Feb-2006 06: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 12:07
imagecopyresized() does not do any resampling. This makes it extremely quick. If quality is an issue, use imagecopyresampled().
MaLaZ
11-Aug-2005 07: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:
<?php
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 "<br>Image Size: ".$_FILES['imagefile']['size']."";
echo "<br>Image Type: ".$_FILES['imagefile']['type']."";
echo "<br>Image Copy Done....<br>";
}
else {
echo "<br><br>";
echo "bad file type (".$_FILES['imagefile']['name'].")<br>";exit;
}
$thumbsize=120;
echo "Thumbnail Info: <br>
1.Thumb defined size: - OK: $thumbsize<br>";
$imgfile = "../images/$imagefile_name";echo "
2.Image destination: - OK: $imgfile<br>";
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($imgfile);
echo "3.Image size - OK: W=$width x H=$height<br>";
$imgratio=$width/$height;
echo "3.Image ratio - OK: $imgratio<br>";
if ($imgratio>1){
$newwidth = $thumbsize;
$newheight = $thumbsize/$imgratio;}
else{
$newheight = $thumbsize;
$newwidth = $thumbsize*$imgratio;}
echo "4.Thumb new size -OK: W=$newwidth x H=$newheight<br>";
$thumb = ImageCreateTrueColor($newwidth,$newheight);
echo "5.TrueColor - OK<br>";
$source = imagecreatefromjpeg($imgfile);
echo "6.From JPG - OK<br>";
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,"../images/thumbs/thumb_$imagefile_name",100);echo "7.Done... - OK<br>";
?>
or without any info, just resizing:
<?php
$thumbsize=120;
$imgfile = "../images/$imagefile_name";
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($imgfile);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $thumbsize;
$newheight = $thumbsize/$imgratio;}
else{
$newheight = $thumbsize;
$newwidth = $thumbsize*$imgratio;}
$thumb = ImageCreateTrueColor($newwidth,$newheight);
$source = imagecreatefromjpeg($imgfile);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,"../images/thumbs/thumb_$imagefile_name",100);
?>
i hope it helps.
FearINC at gmail dot com
10-Aug-2005 10: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.
<?php
function saveThumbnail($saveToDir, $imagePath, $imageName, $max_x, $max_y) {
preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageName, $ext);
switch (strtolower($ext[2])) {
case 'jpg' :
case 'jpeg': $im = imagecreatefromjpeg ($imagePath);
break;
case 'gif' : $im = imagecreatefromgif ($imagePath);
break;
case 'png' : $im = imagecreatefrompng ($imagePath);
break;
default : $stop = true;
break;
}
if (!isset($stop)) {
$x = imagesx($im);
$y = imagesy($im);
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, "{$saveToDir}{$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
07-Aug-2005 02: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).
<?
$image_ratio = $width / $height; $destination_ratio = $max_width / $max_height; if($image_ratio < $destination_ratio)
{
if($height > $max_height)
{
$height = $max_height;
$width = ceil($height / $image_ratio);
}
}
else if ($width > $max_width)
{
$width = $max_width;
$height = ceil($width / $image_ratio);
}
?>
saurabh at saurabh dot com
22-Jul-2005 04:57
URGENT!
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 <dot> tyler <at> gmail <dot> com
12-Jun-2005 03:53
I spent last night getting unsuccessful results from this function until I found this note of fluffle <<at>> 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 100x100 thumbnail.
function ($image_filename, $thumb_location, $image_thumb_size){
//@$image_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.
//@$image_thumb_size - the x-y dimension of your thumb
//in pixels.
list($ow, $oh) = getimagesize($image_filename);
$image_original = imagecreatefromjpeg($image_filename);
$image_thumb = imagecreatetruecolor($image_thumb_size,$image_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($image_thumb, $image_original, 0, 0, $off_w, $off_h, 100, 100, $ow, $oh);
imagejpeg($image_thumb, $thumb_location);
}//end function
no at name dot com
18-May-2005 04: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 function. I hope it will be usefull for someone:
(This is not original code, i'v taked it from somwhere and edited a ltl :)
<?php
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 01: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.
<?php
$filename = 'moon.jpg';
$percent = 1.0; $imagethumbsize = 200; header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize); $image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
imagejpeg($image_p, null, 100);
?>
development at lab-9 dot com
25-Apr-2005 03: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
Here a example:
// 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
$filename = microtime()."_temp.".$extension;
// 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 07:56
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.
/**
* Resize an image and keep the proportions
* @author Allison Beckwith <allison@planetargon.com>
* @param string $filename
* @param integer $max_width
* @param integer $max_height
* @return image
*/
function resizeImage($filename, $max_width, $max_height)
{
list($orig_width, $orig_height) = getimagesize($filename);
$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;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0,
$width, $height, $orig_width, $orig_height);
return $image_p;
}
haker4o at haker4o dot org
26-Feb-2005 08:57
<?php
$picname = resizepics('picture-name.format', '180', '140');
echo $pickname;
die( "<font color=\"#FF0066\"><center><b>File not exists :(<b></center></FONT>");
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 08:37
Resize image proportionaly where you give a max width or max height
<?php
header('Content-type: image/jpeg');
$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 05: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. ;)
<?php
$dir_name = "FooBar"; $olddir = getcwd(); $dir = opendir($dir_name); if(isset($_GET['delpic'])){
chdir('images');
$delpic = $_GET['delpic'];
@unlink($delpic);
chdir($olddir);
}
if(isset($_GET['resize'])){
$percent = ($_GET['resize']/100);
chdir('images');$filename = $_GET['resizepic'];
$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!=''){ list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
switch($format){
case 'image/jpeg':
$source = imagecreatefromjpeg($filename);
break;
case 'image/gif';
$source = imagecreatefromgif($filename);
break;
}
$thumb = imagecreatetruecolor($newwidth,$newheight);
imagealphablending($thumb, false);
$source = @imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
@imagejpeg($thumb, $filename);
chdir($olddir);
header('Location: foobar.php');
}
}
?>
thomas at dermueller dot com
10-Feb-2005 01: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 didn't work for me.
Instead of this line:
$source = @function_image_create($imgfile);
use this line:
$source = @$function_image_create($imgfile);
marcy DOT xxx (AT) gmail.com
02-Jan-2005 11:06
This example allow to use every kind of image and to resize images with ImageCopyResized(), maintaining proportions..
<?php
$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);
$newheight = 80;
$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);
@$function_image_new($thumb);
?>
backglancer at hotmail
13-Dec-2004 07:10
Neat script to create a thumbnails no larger than 150 (or user-specific) height AND width.
<?PHP
$picture ="" $max=150; $src_img=ImagecreateFromJpeg($picture);
$oh = imagesy($src_img); $ow = imagesx($src_img); $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;
}
$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 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.
| |