Black images after resizing - php

Here is my code :
foreach($_FILES as $key=>$photo){
if ($check[$key] == 'OK'){
$path = '../images/chevaux/' . $_POST['horseName'] . '_' . $horseID . '/' . $key . '.' . $file_extension;
$folder = '../images/chevaux/' . $_POST['horseName'] . '_' . $horseID;
if (!is_dir($folder))
{
mkdir($folder);
}
$filename = $photo['tmp_name'];
$percent = 4.08;
list($width, $height) = getimagesize($filename);
$newwidth = $width / $percent;
$newheight = $height / $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename, $path);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $path);
//....
All is working fine. Except that my pictures are completely black. The aim is to reduce their width and height by dividing them by 4.08. Know that uploaded image have width equal to 3264 px and height equal to 2448 px. Maybe it is too much?

imagecreatefromjpeg() takes only 1 parameter, the path to the filename (Either local path or URL). You're giving it two parameters.

Related

Php image resampling dimension issue

Please i am using the below php image function to resize image when uploading or while displaying the image on page, but i found out that when i tried to resize an original image it doesn't give me the actual dimension that i want.
Example: the original image width is 600 and height is 400
I intend to resize it to 180 X 180, but it gave me width 180 and height 120. Please i don't know what the problem is can anyone help?
<?php
function CroppedThumbnail($imagename, $meta, $newpath, $imgwidth, $imgheight, $rename, $imgQuality){
// Set a maximum height and width
$width = $imgwidth;
$height = $imgheight;
$url = '';
//Get image info
$info = #getimagesize($imagename);
$fileparts = pathinfo($imagename);
// Get new dimensions
$imageAvatar = substr($fileparts['filename'],0,5) . '-' . $imgwidth . 'x' . $imgheight . '.png';
if(is_dir($newpath . '/') && file_exists($newpath . '/' . $imageAvatar)){
return $url . $newpath . '/' . $imageAvatar;
}else{
list($width_orig, $height_orig) = $info;
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
if ($info['mime'] == 'image/jpeg'){
$image = imagecreatefromjpeg($url.$imagename);
}
else if ($info['mime'] == 'image/gif'){
$image = imagecreatefromgif($url.$imagename);
}
else if ($info['mime'] == 'image/png'){
$image = imagecreatefrompng($url.$imagename);
}
$image_p = imagecreatetruecolor($width, $height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//Rename the image
if($rename == true){
$newName = substr($fileparts['filename'],0,5) . '-' . $imgwidth . 'x' . $imgheight;
$newFileName = $newpath . '/' . $newName . '.png';
}else{
$newFileName = $newpath . '/' . $imagename;
}
// Output
imagejpeg($image_p, $newFileName, $imgQuality);
return $url . $newFileName;
}
}
This because you are keeping the original aspect ratio of the image.
This is in the following line of code:
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you don't want to keep the aspect ration just delete those lines.

How to trim image with php?

This is my index.php file, i have uploaded and moved image from tmp folder to image folder and it's working properly. but now i want to trim image on display, every picture should be display of same size, i have tried lots of way but it's not working.
Need for help!!
<?php
require_once('appvars.php');
include 'db_connect.php';
$sql = "SELECT * FROM gitarwar";
$data = mysqli_query($dbc, $sql);
echo "<table>";
while ($row = mysqli_fetch_array($data))
{
echo '<tr><td>';
echo '<strong>' . $row['Score'] . '</strong><br/>';
echo '<strong>Name:</strong>' . $row['Name'] . '<br/>';
echo '<strong>Datetime:</strong>' . $row['Datetime'] . '<br/>';
echo '<img src="' .GW_UPLOADPATH . $row['Screenshot'] . '" alt="Score image" />';
echo "</tr>";
}
echo"</table>";
mysqli_close($dbc);
?>
I had a similar task before. My job was to check the image, and if it is smaller than my $newWidth or $newHeight, it added.
$imageUrl = [PATH OR URL TO YOUR IMAGE FILE];
$imageContent = file_get_contents($imageUrl);
$im = imagecreatefromstring($imageContent);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = 300;
$newheight = 300;
$output = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($output, false);
$transparency = imagecolorallocatealpha($output, 0, 0, 0, 127);
imagefill($output, 0, 0, $transparency);
imagesavealpha($output, true);
imagecopymerge($output, $im, ($width < 300 ? (300 - $width) / 2 : 0), ($height < 300 ? (300 - $height) / 2 : 0), 0, 0, $width, $height, 100);
//Show the image
header('Content-Type: image/png');
imagepng($output); //You can save it with another parameter what is a path
imagedestroy($output);
imagedestroy($im);
You need to change this line:
imagecopymerge($output, $im, ($width < 300 ? (300 - $width) / 2 : 0), ($height < 300 ? (300 - $height) / 2 : 0), 0, 0, $width, $height, 100);
to your size.
It's actually not exactly what you want, but it could be a good starting point.
You can trim the image when uploading it, by using imagecreatefrom(jpeg|png|gif) function.
$file = "images/" . $_FILES['image']['name'];
$tmp_file = $_FILES['image']['tmp_name'];
$type = $_FILES['images']['type'];
if (move_uploaded_file($tmp_file, $file)) {
chmod($file, 0777);
// check the type of image, you can do this for other types too, just change the imagecreatefrom function to the type you want to save the image as
if ($type === 'image/jpeg') {
$jpeg = imagecreatefromjpeg($file);
$jpeg_width = imagesx($jpeg);
$jpeg_height = imagesy($jpeg);
$new_width = imagesx($jpeg) / 4; // Fix the width of the thumb nail images, change the width of image here
$new_height = imagesy($jpeg) / 4; // change the height of image here
//new image
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $jpeg, 0, 0, 0, 0, $new_width, $new_height, $jpeg_width, $jpeg_height);
imagejpeg($new_image, $thumb);
chmod($thumb, 0777);
}
}

How can I resize an image from URL using PHP?

I'm receiving Images from URLs and I would like to save these images into a new directory, in three different sizes. I'm already getting the URLs here, now I just need a way to resize each image, with a specific height and width.
I dont want to resize uploaded images, only Images from a specific URL.
My code:
$content = file_get_contents('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
$name = "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg";
$parts = explode('.', $name);
$new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . $parts[count($parts) - 1];
file_put_contents(DIRECTORY.'/' . $new_url , $content);
How can I do that? Thanks.
here is the solution based on imagecopyresampled (GD library) http://php.net/manual/en/function.imagecopyresampled.php
$content = file_get_contents('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
$name = "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg";
$parts = explode('.', $name);
$new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . $parts[count($parts) - 1];
file_put_contents(DIRECTORY.'/' . $new_url , $content);
resizeImage($new_url, DIRECTORY.'/1_' . $new_url, 100, 100);
resizeImage($new_url, DIRECTORY.'/2_' . $new_url, 200, 200);
resizeImage($new_url, DIRECTORY.'/3_' . $new_url, 300, 300);
function resizeImage($source, $dest, $new_width, $new_height)
{
list($width, $height) = getimagesize($source);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($source);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $dest, 100);
}

Compress & save base64 image

My app is receiving base64-encoded image-files from the webbrowser. I need to save them on the client. So I did:
$data = base64_decode($base64img);
$fileName = uniqid() . '.jpg';
file_put_contents($uploadPath . $fileName, $data);
return $fileName;
Which works fine.
Now I need to compress & resize the image to max. 800 width & height, maintaining the aspect-ratio.
So I tried:
$data = base64_decode($base64img);
$fileName = uniqid() . '.jpg';
file_put_contents($uploadPath . $fileName, $data);
return $fileName;
which does not work (error: "imagejpeg() expects parameter 1 to be resource, string given").
And of course, this does compress, but not resize.
Would it be best to save the file in /tmp, read it and resize/move via GD?
Thanks.
2nd part
Thanks to #ontrack I know now that
$data = imagejpeg(imagecreatefromstring($data),$uploadPath . $fileName,80);
works.
But now I need to resize the image to max 800 width and height. I have this function:
function resizeAndCompressImagefunction($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*($r-$w/$h)));
} else {
$height = ceil($height-($height*($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
So I thought I could do:
$data = imagejpeg(resizeAndCompressImagefunction(imagecreatefromstring($data),800,800),$uploadPath . $fileName,80);
which does not work.
You can use imagecreatefromstring
To answer the second part:
$data = imagejpeg(resizeAndCompressImagefunction(imagecreatefromstring($data),800,800),$uploadPath . $fileName,80);
$data will only contain either true or false to indicate wether the operation of imagejpeg was a success. The bytes are in $uploadPath . $fileName. If you want the actual bytes back in $data you have to use a temporary output buffer:
$img = imagecreatefromstring($data);
$img = resizeAndCompressImagefunction($img, 800, 800);
ob_start();
imagejpeg($img, null, 80);
$data = ob_get_clean();

php resize image

I have a class to read and output the image content, if $width is set, it will resize the image, and then output it.
If I call the function like this $image->readImage('123.jpg'); , it can output the image file correctly, but when I call $image->readImage('123.jpg', 300); to resize it, it just display a black image with resized width & height.
And I tried to replace the code from
#imagejpeg($thumb, null, 100);
to
#imagejpeg($image, null, 100);
will works~
-
protected function readImage($fileName, $width = 0)
{
if ($width <= 0) {
return #file_get_contents($this->destination . '/' . $fileName);
} else {
$imageSize = #getimagesize($this->destination . '/' . $fileName);
$actualWidth = $imageSize[0];
$actualHeigth = $imageSize[1];
if ($actualWidth <= $width) {
return #file_get_contents($this->destination . '/' . $fileName);
}
$height = (100 / ($actualWidth / $width)) * .01;
$height = #round($actualHeigth * $height);
$image = #imagecreatefromjpeg($this->destination . '/' . $fileName);
$thumb = #imagecreatetruecolor($width, $height);
#imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $actualWidth, $actualHeight);
ob_start();
#imagejpeg($thumb, null, 100);
$bits = ob_get_contents();
ob_end_clean();
return $bits;
}
}
Any experts know what happened and help me to solve it ?
Thanks.
you've been inconsistant in your spelling of $actualHeight vs $actualHeigth
if you didn't have so many # everywhere, then php would have told you this.

Categories