I have the following class
public static function resize_imagejpg($file, $new_width, $new_height) {
list($orig_width, $orig_height) = getimagesize($file);
$width = $orig_width;
$height = $orig_height;
# taller
if ($height > $new_width) {
$width = ($new_height / $height) * $width;
$height = $new_height;
}
# wider
if ($width > $new_width) {
$height = ($new_width / $width) * $height;
$width = $new_width;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
return $image_p;
}
i'm calling it by
$tmp = Url::resize_imagejpg('uploads/temp.jpg', 300, 300);
then
copy($tmp, 'uploads/thumb.jpg');
but i get the following error
Error on May 10, 2021 21:30PM - copy() expects parameter 1 to be a valid path, resource given in /home....
I know the file is their, i know its an image because i have opened it in an editor but i keep getting that error message
PLEASE HELP!!!
You need to use imagejpeg instead of copy.
Related
I want to create a thumbnail image without black/white bars and have it keep aspect ratio
The thumbnail size should be 320x200 (px).
I actually wrote a function to create a thumbnail for a given resolution but I don't know how to keep the aspect ratio of the image
function imageResize($imageResourceId, $width, $height)
{
$targetWidth = 320;
$targetHeight = 200;
$targetLayer = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
return $targetLayer;
}
But I can't figure out a way to crop them and have them accommodated as I want. Thanks in advance!
To do this you can use imagecopyresampled function like this:
function imageResize($imageResourceId, $width, $height)
{
$targetWidth = 320;
$targetHeight = 200;
$aspectRatio = $width / $height;
$targetRatio = $targetWidth / $targetHeight;
if ($aspectRatio > $targetRatio) {
$newHeight = $targetHeight;
$newWidth = $targetHeight * $aspectRatio;
} else {
$newWidth = $targetWidth;
$newHeight = $targetWidth / $aspectRatio;
}
$targetLayer = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $targetLayer;
}
Using this, the new Width and Height are calculated based on the aspect ratio of the original image.
More samples on: https://www.php.net/manual/en/function.imagecopyresampled.php
I try to upload multiple files to my bucket earlier it worked well but now I try to resize and compress the image before upload, it throws error imagesx() expects parameter 1 to be resource, string given here is the code :
foreach($_FILES as $ind => $filegroup){
$count=0;
foreach($_FILES[$ind]['name']as $actfile){
$tmp=$_FILES[$ind]['tmp_name'][$count];
$count=$count + 1;
$newind= str_replace('_',' ',$ind);
$filenamenew= $insertedids[0].$newind."_".$count.".jpg";
$temp='menu_images/'.$filenamenew;
$im = imagecreatetruecolor(700, 700);
$bg = imagecolorallocate ( $im, 255, 255, 255 );
imagefilledrectangle($im,0,0,700,700,$bg);
$max_width=700;
$max_height= 700;
$width = imagesx($tmp);
$height = imagesy($tmp);
# 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;
}
$im2 = imagescale($tmp, $width, $height);
imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));
imagejpeg($im, NULL , 70);
I am trying to upload this final $im to bucket , I have already installed gd library in my cent os
imagesx() expects an image resource as parameter, returned by one of the image creation functions for example imagecreatetruecolor(). If you need the height or width of of an image, you can use getimagesize() like below.
list( $width, $height ) = getimagesize( 'filepath' );
Try this:
...
foreach($_FILES[$ind]['name'] as $fid => $actfile){ // add $fid in here!
...
list($width, $height) = getimagesize($tmp=$_FILES[$ind]['tmp_name'][$fid]);
echo "width: $width; height: $height<br />\n";
...
}
...
The key problem was posting sourcefile to s3 bucket I changed that to body , also I changed my php settings to allow file upload till 5 mb
Here is the solution:
$im = imagecreatetruecolor(700, 700);
$bg = imagecolorallocate ( $im, 255, 255, 255 );
imagefilledrectangle($im,0,0,700,700,$bg);
list( $width, $height , $image_type ) = getimagesize($tmp);
$max_width=700;
$max_height= 700;
# 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;
}
switch ($image_type)
{
case 1: $im3 = imagecreatefromgif($tmp); break;
case 2: $im3 = imagecreatefromjpeg($tmp); break;
case 3: $im3 = imagecreatefrompng($tmp); break;
default: return ''; break;
}
$im2 = imagescale($im3, $width, $height);
imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));
$imgdata = image_data($im);
function image_data($gdimage)
{
ob_start();
imagejpeg($gdimage,NULL,70);
return(ob_get_clean());
}
And while uploading to bucket
$result = $s3->putObject([
'Bucket' => 'yourbucket',
'Key' => 'filename',
'Body' => $imgdata
]);
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);
}
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
I have successfully resized images using GD library. I resize any image to 350 x 250, the problem is tat some pictures don't look good (stretch) when they are resized as i am resizing them to a fixed size. I have a space of 350 x 250 where resize picture needs to be fit, I don't mind if the pic size is smaller than 350 x 250 as long as it does not stretch. How do i solve this problem?
$save = "$directory/" . $file_name; //This is the new file you saving
$file = "$directory/" . $file_name; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 350;
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
$diff = $width / $modwidth;
$modheight = 250;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100) ;
Try using this function I've written some time ago:
public function resize($img, $width, $height, $stretch = false)
{
$temp = imagecreatetruecolor($width, $height);
imagealphablending($temp, true);
imagesavealpha($temp, true);
$bg = imagecolorallocatealpha($temp, 0, 0, 0, 127); // Background color
imagefill($temp, 0, 0, $bg);
if ($stretch)
{
imagecopyresampled($temp, img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
}
else
{
if (imagesx($img) <= $width && imagesy($img) <= $height)
{
$fwidth = imagesx($img);
$fheight = imagesy($img);
}
else
{
$wscale = $width / imagesx($img);
$hscale = $height / imagesy($img);
$scale = min($wscale, $hscale);
$fwidth = $scale * imagesx($img);
$fheight = $scale * imagesy($img);
}
imagecopyresampled($temp,
$img,
($width - $fwidth) / 2, ($height - $fheight) / 2,
0, 0,
$fwidth, $fheight,
imagesx($img), imagesy($img)
);
}
return $temp;
}
if you say not to stretch the image, it will calculate a new size making it fit your new size.
use it as:
...
$image = imagecreatefromjpeg($file);
$resized = resize($image, 350, 250, false); // false = don't stretch
imagejpeg($resized, $save, 100);
...
now store $resized on the disk using imagepng() for example.