PHP image resize with GD use to many CPU time - php

I have a script on PHP GD with simple functions how to resize on real-time images on my site, but this process "eat" all my CPU time ... I use VPS with 4 cores and it's not enough.
From image tag i call PHP file (<img src="image.php?file=XXX&width=XXX&height=XXX" alt="" />) with image name, new width and new height ... after that in PHP I make this operations:
ob_start();
ini_set('max_execution_time', 0);
set_time_limit(0);
error_reporting(E_ALL^E_NOTICE^E_DEPRECATED);
date_default_timezone_set('Europe/Sofia');
header('Content-type: image/jpeg');
$url = $_GET['file'];
if(!empty($url) && file_exists($url) && is_file($url) && is_readable($url)){
$width = $_GET['width'];
$height = $_GET['height'];
list($width_orig, $height_orig) = getimagesize($url);
if(empty($width)) $width = $width_orig/($height_orig/$height);
if(empty($height)) $height = $height_orig/($width_orig/$width);
$ratio_orig = $width_orig/$height_orig;
if($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
imagedestroy($image_p);
imagedestroy($image);
}
ob_end_flush();
I don't understand why "eat" to match CPU time ... my original image size it's 1920x2880 px.
How to reduce my CPU usage? Any ideas?

Related

How to resize and keep aspect ratio The image cannot be displayed because it contains errors

This is straight out of the PHP manual except the jpg name. I even used the php logo in the php manual. I right clicked and saved image, just to be sure it wasn't my images. I researched other questions in stack overflow, such as GD is enabled. There was also talk of UTF-8 with BOM doesn't work. I use Visual Studio Code with UTF-8 no BOM. Here is the link to page. http://cartrus.com/shops/scale_image.php?
<?php
// The file
$filename = 'php.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>

Resizing image using php

I am trying to resize my image by making it 200 x 200 while keeping the aspect ratio. I am trying to follow http://php.net/manual/en/function.imagecopyresampled.php to try to accomplish this. Here is my code:
<?php
$directory = "uploads/";
$images = glob($directory."*.jpeg");
foreach($images as $filename) {
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
}
?>
I took out the content type because it was giving me an error. Instead of outputting the resized image, it was outputting bunch of symbols like this:
JFIF�����t��jw��E��4."�DN�9>m���4����P};WI��x"�.
Anybody know why this is happening?
You need to sent header to a browser so your output could be properly understand by browser. Add following line to your script:
header('Content-Type: image/jpeg');
You need to have the header('Content-Type: image/jpeg'); before the foreach loop
This code must be at the very beginning of your file:
header('Content-Type: image/jpeg');
And the above code should be standalone, you shouldn't have any HTML tags anywhere and it shouldn't be "PHP-included" anywhere.

How to up-scale an image {PHP}

I used the code below to get an image:
$url = "http://maps.googleapis.com/maps/api/staticmap?size=800x600&zoom=12&center=(10.763705518561297,106.64444580078126)";
$content = file_get_contents($url);
I received a 640px x 640px image meanwhile I expected a 800px x 600px one. How do I scale that image to 800px x 600px?
Try doing this:
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
Here, rescaling is done by 50%, you can change parameter as per your requirement. You can even use Imagick for image manipulation. See, if that helps.
Referred: PHP Manual

resizing photo in php

This is the code provided in an example at: http://www.php.net/manual/en/function.imagecopyresampled.php
test.jpg size: 500x357
phpdotnet_resize.php
This displays test.jpg resized at 200x142
<?php
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
echo "<img src='$filename'>";
?>
However, this displays test.jpg in its original size of 500x357
<?php
include "phpdotnet_resize.php";
echo "<img src='$filename'>";
?>
I am trying to move the resized test.jpg to images/ but it keeps moving the original size image.
upload.php (only relevant snippet showing)
include "phpdotnet_resize.php";
if(move_uploaded_file($file_tmp,"images/".$filename)){
echo "Success";

Using imagejpeg() save to folder but doesn't preview image on the browser

Index.php
<?php
error_reporting(E_ALL);
// File and new size
//the original image has 800x600
$filename = 'images/lazy1.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
// Content type
header('Content-Type: image/jpg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output and free memory
//the resized image will be 400x300
imagejpeg($thumb, "raj.jpg", 100);
imagedestroy($thumb);
echo "Image Resize Successfully";
?>
I am using imagejpeg() to save file. i want save file in folder but not show on browser. in browser only show this message "Image Resize successfully".
<?php
error_reporting(E_ALL);
// File and new size
//the original image has 800x600
$filename = 'images/lazy1.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
//Removed lines here
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output and free memory
//the resized image will be 400x300
imagejpeg($thumb, "raj.jpg", 100);
imagedestroy($thumb);
echo "Image Resize Successfully";
?>

Categories