I have this piece of code here
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
Baiscally what I am trying to do is upload an image and resize the width and have the height adjusted based on the width.
I tried this also
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width);
without the height and got this error
Warning: Wrong parameter count for imagecopyresampled() in /home/content/44/8713044/html/admin/Categories.php on line 63
this is the current code where the $width and $height variables come from.
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
Any Help would be appreciated, Thanks in advanced,
J
Here is the full function..
function create_thumbnail($source,$destination, $thumb_width) {
$percent = 0.5;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream');
$extension = get_image_extension($source);
if($extension=='jpg' || $extension=='jpeg')
$image = imagecreatefromjpeg($source);
if($extension=='gif')
$image = imagecreatefromgif($source);
if($extension=='png')
$image = imagecreatefrompng($source);
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if($extension=='jpg' || $extension=='jpeg')
imagejpeg($new_image,$destination);
if($extension=='gif')
imagegif($new_image,$destination);
if($extension=='png')
imagepng($new_image,$destination);
}
the $thumb_width is 600 and this returns my image 600*600
This worked perfectly when i tested it .... Except you want to work with fixed size
$filename = "a.jpg" ;
$percent = 0.5;
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);
Demo
Related
I'm trying to modify an upload script, at the moment I can crop an image into a square while resizing - great!
However I would like the user to be able to upload any size image, and for the script to create 200x150, 400x300, 800x600 thumbnail/images - a 4:3 ratio.
My code so far is:
list($width,$height) = getimagesize($uploadedfile);
if ($thumb == 1){
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail
$thumbSize = 200;
$tmp = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($tmp, $src, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
// write thumbnail to disk
$write_thumbimage = $folder .'/thumb-'. $image;
switch($ext){
case "gif":
imagegif($tmp,$write_thumbimage);
break;
case "jpg":
imagejpeg($tmp,$write_thumbimage,100);
break;
case "jpeg":
imagejpeg($tmp,$write_thumbimage,100);
break;
case "png":
imagepng($tmp,$write_thumbimage);
break;
}
Does anyone know the required formula or can point me in the right direction?
Worked out the logic using some of what was on the C# duplicate:
$thumb_width = 200;
$thumb_height = 150;
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ) {
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$tmp = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($tmp,
$src,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
not work?
$image = imagecreatefrompng("$filename");
My php code for crop photo
$filename="yaaa.png";
$width: 10px;
$height: 10px;
$image = imagecreatefrompng("$filename");
$thumb_width = $width;
$thumb_height = $height;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2,
0 - ($new_height - $thumb_height) / 2,
0, 0,
$new_width, $new_height,
$width, $height);
imagepng($thumb, $filename, 100);
imagedestroy($thumb);
im run this code but not working, my pict is dark and text "picture not valid"
why?
The definition of $width and $height is invalid. Change it like this:
$width = 10;
$height = 10;
I have this resize and crop function :
$file = imagecreatefromjpeg($imagefile["tmp_name"]);
if($imagefile["type"] == "image/jpg" || $imagefile["type"] == "image/jpeg") {
$uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
} elseif($imagefile["type"] == "image/png") {
$uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
}
imagejpeg($file, $uploaded_file, 100);
chmod($uploaded_file, 0644);
list($width, $height, $type, $attr) = getimagesize($uploaded_file);
$this->imageSizeW = $width;
$this->imageSizeH = $height;
$this->newSizeW = 250;
$this->newSizeH = 400;
$aspect = $width / $height;
$new_aspect = $this->newSizeW / $this->newSizeH;
if($aspect >= $new_aspect) {
$new_height = $this->newSizeH;
$new_width = $width / ($height / $this->newSizeH);
} else {
$new_width = $this->newSizeW;
$new_height = $height / ($width / $this->newSizeW);
}
$img_source = imagecreatefromjpeg($uploaded_file);
$newimage = imagecreatetruecolor($this->newSizeW, $this->newSizeH);
imagecopyresampled($newimage, $img_source, 0 - ($new_width - $this->newSizeW) / 2, 0 - ($new_height - $this->newSizeH), 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newimage, $uploaded_file, 100);
}
That works as intended, almost - when it crops it removes part of the image.
Can I somehow extend / configure this function so that it will resize / center the image and add whitespace so the image will be as original but still 250 x 400 px
Using Class upload 0.32 from voret.net instead - this does what I want!
I have an upload form where you select photos. Upon upload I resize the image if necessary.
It seems any photo that I upload where the HEIGHT > WIDTH stretches the image. If I upload an image where WIDTH > HEIGHT it works fine. I've been racking my brain trying to figure this out. I'm pretty sure I know which line is the issue and I've pointed it out in a comment.
Can anyone see what is wrong with my math? Thanks!
<?php
$maxWidth = 900;
$maxHeight = 675;
$count = 0;
foreach ($_FILES['photos']['name'] as $filename)
{
$uniqueId = uniqid();
$target = "../resources/images/projects/" . strtolower($uniqueId . "_" . $filename);
$file = $_FILES['photos']['tmp_name'][$count];
list($originalWidth, $originalHeight) = getimagesize($file);
// if the image is larger than maxWidth or maxHeight
if ($originalWidth > $maxWidth || $originalHeight > $maxHeight)
{
$ratio = $originalWidth / $originalHeight;
// I think this is the problem line
(($maxWidth / $maxHeight) > $ratio) ? $maxWidth = $maxWidth * $ratio : $maxHeight = $maxWidth / $ratio;
// resample and save
$image_p = imagecreatetruecolor($maxWidth, $maxHeight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $maxWidth, $maxHeight, $originalWidth, $originalHeight);
$image = imagejpeg($image_p, $target, 75);
}
else
{
// just save the image
move_uploaded_file($file,$target);
}
$count += 1;
}
?>
When scaling, you need to modify both the width and the height of the target.
Try:
if ($originalWidth > $maxWidth || $originalHeight > $maxHeight)
{
if ($originalWidth / $maxWidth > $originalHeight / $maxHeight) {
// width is the limiting factor
$width = $maxWidth;
$height = floor($width * $originalHeight / $originalWidth);
} else { // height is the limiting factor
$height = $maxHeight;
$width = floor($height * $originalWidth / $originalHeight);
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
$image = imagejpeg($image_p, $target, 75);
}
im looking to create thumbnails that has 100px by 100px dimension. i've seen many articles explaining the methods but most end up having the width!=height if the dimension ratio is to be kept.
for example, i have a 450px by 350px image. i would like to crop to 100px by 100px. if i were to keep the ratio, i would end up having 100px by 77px. this makes it ugly when im listing these images in a row and column. however, a image without dimension ratio will look terrible as well.
i've seen images from flickr and they look fantastic. for example:
thumbnail: http://farm1.static.flickr.com/23/32608803_29470dfeeb_s.jpg
medium size: http://farm1.static.flickr.com/23/32608803_29470dfeeb.jpg
large size: http://farm1.static.flickr.com/23/32608803_29470dfeeb_b.jpg
tks
This is done by only using a part of the image as the thumbnail which has a 1:1 aspect ratio (mostly the center of the image). If you look closely you can see it in the flickr thumbnail.
Because you have "crop" in your question, I'm not sure if you didn't already know this, but what do you want to know then?
To use cropping, here is an example:
//Your Image
$imgSrc = "image.jpg";
//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);
//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);
Crop image with square based on lesser width or height
public function croppThis($target_url) {
$this->jpegImgCrop($target_url);
}
$target_url - is Name of image.
public function jpegImgCrop($target_url) {//support
$image = imagecreatefromjpeg($target_url);
$filename = $target_url;
$width = imagesx($image);
$height = imagesy($image);
$image_type = imagetypes($image); //IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM
if($width==$height) {
$thumb_width = $width;
$thumb_height = $height;
} elseif($width<$height) {
$thumb_width = $width;
$thumb_height = $width;
} elseif($width>$height) {
$thumb_width = $height;
$thumb_height = $height;
} else {
$thumb_width = 150;
$thumb_height = 150;
}
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect ) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else {
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
You can use this code.
You need to pass source image path and thumbnail size in px, and optional destination path. if you pass it will save the image else the thumb will be shown.
Only jpg, jpeg and png are allowed.
function cropImage($sourcePath, $thumbSize, $destination = null) {
$parts = explode('.', $sourcePath);
$ext = $parts[count($parts) - 1];
if ($ext == 'jpg' || $ext == 'jpeg') {
$format = 'jpg';
} else {
$format = 'png';
}
if ($format == 'jpg') {
$sourceImage = imagecreatefromjpeg($sourcePath);
}
if ($format == 'png') {
$sourceImage = imagecreatefrompng($sourcePath);
}
list($srcWidth, $srcHeight) = getimagesize($sourcePath);
// calculating the part of the image to use for thumbnail
if ($srcWidth > $srcHeight) {
$y = 0;
$x = ($srcWidth - $srcHeight) / 2;
$smallestSide = $srcHeight;
} else {
$x = 0;
$y = ($srcHeight - $srcWidth) / 2;
$smallestSide = $srcWidth;
}
$destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
if ($destination == null) {
header('Content-Type: image/jpeg');
if ($format == 'jpg') {
imagejpeg($destinationImage, null, 100);
}
if ($format == 'png') {
imagejpeg($destinationImage);
}
if ($destination = null) {
}
} else {
if ($format == 'jpg') {
imagejpeg($destinationImage, $destination, 100);
}
if ($format == 'png') {
imagepng($destinationImage, $destination);
}
}
}
I like using GDLib to fiddle with images, it's fantastically easy to work with also. There are lots of blog posts and tutorials out there. I would recommend using a class for it or similar though, as taking care of all the variations in the image can be very time consuming.
To complete #SvenKoschnicke code, here is little tool to process other image formats:
$sourceProperties = getimagesize($imgSrc);
$width = $sourceProperties[0];
$height = $sourceProperties[1];
switch ($sourceProperties[2]) {
case IMAGETYPE_PNG:
$myImage = imagecreatefrompng($imgSrc);
break;
case IMAGETYPE_GIF:
$myImage = imagecreatefromgif($imgSrc);
break;
case IMAGETYPE_JPEG:
$myImage = imagecreatefromjpeg($imgSrc);
break;
}