"FancyResize" PHP script - destroys images - php

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);
?>

Related

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?

php read and write an image in url

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;}

Image resize issue using GD

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.

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.

Resize Image PNG With transparence

I want to resize an image PNG with transparence plz help.
Here is the code :
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
Try this
UPDATED
function createThumb($upfile, $dstfile, $max_width, $max_height){
$size = getimagesize($upfile);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height)) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if($size['mime'] == "image/jpeg"){
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imageinterlace( $dst, true);
ImageJpeg($dst, $dstfile, 100);
} else if ($size['mime'] == "image/png"){
$src = ImageCreateFrompng($upfile);
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($src, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($src, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($src, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($src, true);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
Imagepng($dst, $dstfile);
} else {
$src = ImageCreateFromGif($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imagegif($dst, $dstfile);
}
}
There a simple to use, open source library called PHP Image Magician. It uses GD and supports transparency
Example of basis usage:
$magicianObj = new imageLib('racecar.png');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');

Categories