php read and write an image in url - php

I would like to resize an image in URL such as localhost/changimage.php?percent=40. I have already resized it in php. But user should resize it by typing in URL
here is my PHP code:changimage.php
<?php
$filename = '/var/www/html/zahir/images/bike.jpeg';
$percent = 0.5;
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$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);
?>
I have no idea how to do it in URL.

OR
$percent = 0.5;
if (isset($_GET['percent'])) {$percent = $_GET['percent']/100;}
To check if percent has a value

Try it.
$percent = 0;
if (isset($_GET["percent"])) {
$percent = ($_GET["percent"] / 100);
}
if ($percent <= 0) {
$percent = 1;
}
$filename = '/home/deepakgupta/Pictures/phone.jpg';
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$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);

maybe something like that:
$percent = 0.5;
if ($_GET['percent']) {$percent = $_GET['percent']/100;}

Related

Not true color with imagecreatetruecolor & imagecreatefromjpeg

Hy there,
I use this code to resize my images to improve size...
<?php
// File
$filename = 'ok.jpg';
list($width, $height) = getimagesize($filename);
$ratio = ($width >= $height) ? 683 : 1152;
$percent = $ratio / $height;
// New sizes
$new_width = $width * $percent;
$new_height = $height * $percent;
// Apply new sizes
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save
imagejpeg($image_p, 'tes.jpg', 80);
imagedestroy($image);
imagedestroy($image_p);
But I saw a problem with the colors quality... image looks more grey than original.
Original picture :
And after resizing :
What can I do to keep same colors ?
Thanks

Adding logo as watermark, watermark poor quality

I am adding a transparant logo as watermark over an image using PHP. However, in the result the logo has poor quality (the image that is under it is high quality, so it's just the watermark). This is the code I use (its about the last 3 lines):
header("Content-Type: image/png");
$photo = imagecreatefromjpeg('photos/'.$photo['image']);
$height = imagesx($photo);
$width = imagesx($photo);
if ($width > $_POST['width']) {
$r = $width / $_POST['width'];
$newwidth = $width / $r;
$newheight = $height / $r;
}
$image = imagecreatetruecolor($width, $height);
$image2 = imagecopyresampled($image, $photo, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$position = explode(" ", $_POST['background']);
$image3 = imagecrop($image, [
'x' => str_replace(array('-', 'px'), array('', ''), $position[0]),
'y' => str_replace(array('-', 'px'), array('', ''), $position[1]),
'width' => $_POST['width'],
'height' => $_POST['height']
]);
$stamp = imagecreatefrompng('img/logo.png');
imagecopyresized($image3, $stamp, 0, 0, 0, 0, 147, 50, imagesx($stamp), imagesy($stamp));
imagepng($image3, "created/".time().".png", 9);
imagecopyresized will copy and scale and image. This uses a fairly primitive algorithm that tends to yield more pixelated results.
a simple example for a better quality is:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
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);
// Output
imagejpeg($image_p, null, 100);
?>
you should have a look at this post here
use quality of image from 1-100.
imagejpeg($image, $new_image_name, 99);

PHP reduce size and quality with multiple images

this function work only with one picture. The function work only if I call the function once.
function imgToThumbnail($name) {
$filename = $name;
$percent = 0.05;
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$thumbnail=imagejpeg($image_p, null, 90);
imagedestroy($image_p);
return $thumbnail;
}
imgToThumbnail("01.jpg"); // ok
imgToThumbnail("02.jpg"); // not working
I forgot something?

"FancyResize" PHP script - destroys images

I have a script from an old website called FancyResize, that resizes images to a new requested width and height. For some reason it now "destroys" images when used - not every time, but in many circumstances it makes a weird color overlay, changes pixels etc., so it is impossible to see what image it actually should have been. I guess it could have something to do with new PHP version etc., but I don't know, but I guess it have worked before.
So, my question is: can anybody see a mistake in this?
<?
$new_width = $_REQUEST[w];
$new_height = $_REQUEST[h];
$file = $_REQUEST[i];
$localpath = "WEBSITENAME";
$query = $_SERVER[QUERY_STRING];
$datafile = $_SERVER[DOCUMENT_ROOT].'/img/fronttemp/'.md5($query);
if (file_exists($datafile))
{
header("Content-type: image/jpeg");
die(file_get_contents($datafile));
}
if (substr($file, 0, strlen($localpath)) == $localpath)
{
$file = substr($file, strlen($localpath));
$file = $_SERVER[DOCUMENT_ROOT].$file;
}
else
{
$file = substr($query, strpos($query, "&i=")+3);
}
$image = imagecreatefromjpeg($file);
if (!$image)
{
$image =imagecreatefromgif($file);
$is_gif = true;
}
else $is_gif = false;
if (!$image) die("error reading gif/jpg $file");
$info = getimagesize($file) or die("error getting info");
$width = $info[0];
$height = $info[1];
$input_landscape = ($width/$height)>1;
$output_landscape = ($new_width/$new_height)>1;
function imageScaleToWidth($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$scale_height = ceil(($new_width/$width)*$height);
$scaledimage = imagecreatetruecolor($new_width, $scale_height);
$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $new_width, $scale_height, $width, $height);
return $scaledimage;
}
function imageScaleToHeight($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$scale_width = ceil(($new_height/$height)*$width);
$scaledimage = imagecreatetruecolor($scale_width, $new_height);
$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $scale_width, $new_height, $width, $height);
return $scaledimage;
}
function makeImageWideEnough($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
if ($width < $new_width)
{
$scale_height = ceil(($new_width/$width)*$height);
$scaledimage = imagecreatetruecolor($new_width, $scale_height);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $new_width, $scale_height, $width, $height);
return $scaledimage;
}
return $image;
}
function makeImageHighEnough($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
if ($height < $new_height)
{
$scale_width = ceil(($new_height/$height)*$width);
$scaledimage = imagecreatetruecolor($scale_width, $new_height);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $scale_width, $new_height, $width, $height);
return $scaledimage;
}
return $image;
}
function imageCrop($image, $new_height, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$croppedimage = imagecreatetruecolor($new_width, $new_height);
$sx = ($width-$new_width)/4;
$sy = ($height-$new_height)/4;
imagecopy($croppedimage, $image, 0, 0, $sx, $sy, $new_width, $new_height);
return $croppedimage;
}
function scaleWidth($image, $new_width)
{
$width = imagesx($image);
$height = imagesy($image);
$ratio = $new_width / $width;
$sx = ceil($width*$ratio);
$sy = ceil($height*$ratio);
$scaledimage = imagecreatetruecolor($sx, $sy);
$white = imagecolorallocate($scaledimage, 255, 255, 255);
imagecolortransparent($dest, $scaledimage);
imagecopyresampled($scaledimage, $image, 0,0,0,0, $sx, $sy, $width, $height);
return $scaledimage;
}
$croppedimage = scaleWidth($image, $new_width);
if ($new_height < imagesy($croppedimage))
{
$croppedimage = imageCrop($croppedimage,$new_height,$new_width);
}
header("Content-type: image/jpeg");
imagejpeg($croppedimage, $datafile);
echo file_get_contents($datafile);
?>

download image from remote source and resize then save

Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.

Categories