|
|
imagejpeg (PHP 3 >= 3.0.16, PHP 4, PHP 5) imagejpeg -- Output image to browser or file Описаниеbool imagejpeg ( resource image [, string filename [, int quality]] )
imagejpeg() creates a JPEG file from
the given image.
Список параметров
imageAn image resource, returned by one of the image creation functions,
such as imagecreatetruecolor(). filename
The path to the saved file. If not set or NULL, the raw image stream
will be outputed directly.
To skip this argument in order to provide the
quality parameter, use NULL.
quality
quality is optional, and ranges from 0 (worst
quality, smaller file) to 100 (best quality, biggest file). The
default is the default IJG quality value (about 75).
Возвращаемые значения
Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.
ПримечанияЗамечание: JPEG support is only available if
PHP was compiled against GD-1.8 or later.
Замечание:
If you want to output Progressive JPEGs, you need to set interlacing
on with imageinterlace().
Michaelsoft
16-Oct-2007 02:50
I could not find any information on changing the DPI information on a JPG file using the GD lib. Since changing this does not resize or scale the actual image, it is only a header-setting.
The following snipplet will save your $image to $file and set the DPI to 150.
<?php
imagejpeg($image, $file, 75);
$dpi_x = 150;
$dpi_y = 150;
$size = filesize($file);
$image = file_get_contents($file);
$image[13] = chr(1);
$image[14] = chr(floor($dpi_x/255));
$image[15] = chr( $dpi_x%255);
$image[16] = chr(floor($dpi_y/255));
$image[17] = chr( $dpi_y%255);
$f = fopen($file, 'w');
fwrite($f, $msg, $size);
fclose($f);
?>
P.s. not fully tested (yet) but it works for my images ...
Elliott Brueggeman
30-Jul-2007 09:37
I did an experiment with the image quality parameter of the imagejpeg() function when creating jpegs. I found the optimal image quality with file size is taken into account to be 80 - very close to the default value of 75.
Anything over 80 results in an unnecessary increase in file size without much increase in image quality.
Results and sample pictures: http://www.ebrueggeman.com/article_php_image_optimization.php
write2shadi [at] gmail [dot] com
30-Jul-2007 06:54
after seeking for 2 days why ImageJpeg() was writing an empty file to the server, it was due to insufficient disk space on my hosting plan.... hope this helps,
Pedja (pedja at supurovic dot net)
26-Jul-2007 10:59
Here is sample function that creates thumbnail of source JPEG file. Thumbnail wil be in square form (with and height are the same), and original image cropped to fit in.
Parameters:
$p_thumb_file - name of the file (including path) where thumb should be saved to
$p_photo_file - nam of the source JPEG file (including path) thatthumbnail should be created of
$p_max_size - with and height (they will be the same) in pixels for thumbnail image
$p_quality - quality of jpeg thumbnail
<?php
function photoCreateCropThumb ($p_thumb_file, $p_photo_file, $p_max_size, $p_quality = 75) {
$pic = @imagecreatefromjpeg($p_photo_file);
if ($pic) {
$thumb = @imagecreatetruecolor ($p_max_size, $p_max_size) or die ("Can't create Image!");
$width = imagesx($pic);
$height = imagesy($pic);
if ($width < $height) {
$twidth = $p_max_size;
$theight = $twidth * $height / $width;
imagecopyresized($thumb, $pic, 0, 0, 0, ($height/2)-($width/2), $twidth, $theight, $width, $height);
} else {
$theight = $p_max_size;
$twidth = $theight * $width / $height;
imagecopyresized($thumb, $pic, 0, 0, ($width/2)-($height/2), 0, $twidth, $theight, $width, $height);
}
ImageJPEG ($thumb, $p_thumb_file, $p_quality);
}
}
?>
Ross
27-Apr-2007 08:58
Just incase its confusing, i forgot to add
$thumbwidth=70;
$thumbheight=70;
to the top of the script below!
Ross
27-Apr-2007 07:53
This is a function that I had developed for a CMS. The idea is that it runs from an upload form. The user uploads a hi-res image, which is copied to the server, a low res one is created (half the width/height of hi res) and a thumbnail is created.
The image is resized matching the smallest side of the image to the thumbnail size (in this example it is 70px). The other side is then resized proportionally (which will end up being more than 70px) and cropped and centered so that it too is 70px.
There is a mixture of imagecopyresized and imagecopyresampled, i have chosen the ones that work best in this situation but imagecopyresampled gives a much nicer image.
Hope this helps someone.
$dest_hires = "../images/artistsphotos/".$imageName."h.jpg";
$dest_lowres = "../images/artistsphotos/".$imageName."l.jpg";
$dest_thumbnail = "../images/artistsphotos/".$imageName."i.jpg";
/*upload full size image to site - if it is hi res we do not want to manipulate it
want to leave all qualit in tact.*/
move_uploaded_file($_FILES["fileartimg"]["tmp_name"],"$dest_hires");
$ims = getimagesize($dest_hires); //now we have dimensions of original image...
/******creating lower res image******/
$newwidth=ceil($ims[0]/2);//half the width of original file - use ceil() to avoid decimals.
$newheight=ceil($ims[1]/2);//half the height
$img = imagecreatetruecolor($newwidth,$newheight); //low res img - always use truecolor to prevent any 'wierd' colour effects.
$org_img = imagecreatefromjpeg($dest_hires); //load in hi res
imagecopyresized($img, $org_img, 0, 0, 0, 0, $newwidth, $newheight, $ims[0], $ims[1]);
imagejpeg($img,$dest_lowres,80);//save to file low res img.
imagedestroy($img);
/******creating thumbnail******/
//see which is bigger, x or y axis, then resize smaller side to 70.
//$resizewidth is the temporary width of the thumbnail, it is in fact the width when it has been resized
//using the apect ration and before cropping, at this stage the thumb will not be 70px X 70px
//unless it is a square. Same applies to height ($resizeheight).
//$thumbx and $thumby are the positions of the cropping are for thumbnail, these are calculated
//so that the cropped image is centered.
if ($ims[0]>$ims[1])
{//then the width is bigger
$aspectRatio = $ims[1]/70;
$resizewidth=ceil($ims[0]/$aspectRatio);
$resizeheight=70;
$thumbx=ceil(($resizewidth - $thumbwidth)/2);
$thumby=0;
}
else if ($ims[0]<$ims[1])
{//then the height is bigger
$aspectRatio = $ims[0]/70;
$resizewidth=70;
$resizeheight=ceil($ims[1]/$aspectRatio);
$thumbx=0;
$thumby=ceil(($resizeheight - $thumbheight)/2);
}
else if ($ims[0]==$ims[1])
{//then we have a perfect square.
$resizewidth=70;
$resizeheight=70;
$thumbx=0;
$thumby=0;
}
$img = imagecreatetruecolor($resizewidth,$resizeheight);
$org_img = imagecreatefromjpeg($dest_lowres);
//this is the almost thumbnail sized image with everything resized to ratio
imagecopyresampled($img, $org_img, 0, 0, 0, 0, $resizewidth, $resizeheight, $newwidth, $newheight);
$img2 = imagecreatetruecolor($thumbwidth,$thumbheight);
//this is the thumbnail image, where the above is cropped.
imagecopyresized($img2, $img, 0, 0, $thumbx, $thumby, $resizewidth, $resizeheight, $resizewidth, $resizeheight);
imagejpeg($img2,$dest_thumbnail,100);
imagedestroy($img);
imagedestroy($img2);
john at mtslink dot com
04-Jan-2007 08:07
Just wanted to mention that the create_thumbnail script below fails on uppercase filenames. Many cameras default to IMG_XX.JPG and since strpos is case sensitive it fails.
I changed all the strpos to stripos and it worked wonderfully.
webmaster at jongliertreff dot de
25-Aug-2006 04:56
Here's another on-the-fly thumbnail creation script.
When I scripted the pictuerviewer on my page, I had all the pictures only in full size and qualit, because I wanted the posibility f
|
|