Select an image form datebase and resize it - php

The code below will resize an image to 50*50 but whenever i select the name of the image in my database and put it like these $filename = 'folder/$imagename'; it does not work how can i fix these.
<?php
$filename = 'folder/Aizen.jpg';
$width = 50;
$height = 50;
header('Content-type: image/jpeg');
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;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
?>
<img src="<?= $filename ?>" alt="" />

<?php
/*
Database Query
*/
$myImageFromTableData = $row['Image'];
/*
Start my Code
*/
$filename = $myImageFromTableData;
Just one small issue. You have not shown your database code.

You can't use variables in single quote strings. Try it like this.
$filename = 'folder/'.$imagename;

Related

Resize image while uploading to amazon s3

I need to resize image to 150 x 150 px and then upload it to Amazon S3
Following is the code:
$image = $_FILES["userImage"]["name"];
$fileTempName = $_FILES['userImage']['tmp_name'];
$new_width = 150;
$new_height = 150;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromstring(file_get_contents($fileTempName));
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));
$newFielName = tempnam(sys_get_temp_dir(), "tempfilename");
imagepng($image_p, $newFielName, 9);
$s3 = new S3(awsAccessKey, awsSecretKey);
//move the file
if ($s3->putObjectFile($fileTempName, "urimages", $newFielName, S3::ACL_PUBLIC_READ)) {
$image_link = 'https://s3-us-west-2.amazonaws.com/urimages/' . $newFielName . '';
$this->Product->saveField('image', $image_link);
}
Following is the link which i receive upon uploading : https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp
Could be you please help me debug the code
i think issue in path. Please create folder on s3 and make a valid
path according to that folder
https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp
Example :- Resampling an image proportionally
<?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);
?>

Magento : add custom php code into product description

I wrote a php script that resizes jpeg images provided by external URL.
The script works well on my server with a test page, but it seems that Magento doesn't allow custom php code into product description.
Is there any way to insert that piece of code into description ?
<?php
header('Content-Type: text/html; charset=utf-8');
$url_A = 'http:/...';
$url_B = 'http:/...';
$resizer_URL = 'http:/...';
echo '<img class="scanA" src="'.$resizer_URL.'?url='.$url_A.'"/>
<img class="scanB" src="'.$resizer_URL.'?url='.$url_B.'"/>';
?>
The script code is:
<?php
// The file
$filename = $_GET['url'];
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$ratio = $height / $width;
$new_width = 580;
$new_height = $new_width * $ratio;
// 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, 75);
//
imagedestroy($image_p);
imagedestroy($image);
?>
Thanks.

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

header(Content-type: image/jpeg) not working

I have written some code to resize an image (followed PHP manual) but I cannot get the image to display. I know the image data exists as I can see it when I echo the $thumb but I can't seem to get the actual image to display.
This is the code I have used:
$row = mysql_fetch_array($result);
$image = $row["image"];
echo $image;
echo $row["name"];
list($width, $height) = getimagesize($image);
$newwidth = $width * 0.1;
$newheight = $height * 0.1;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($image);
imagecopyresized($thumb, $source, 0,0,0,0, $newwidth, $newheight, $width, $height);
header('Content-Length: '.strlen($thumb), true);
header("Content-type: image/jpeg");
echo imagejpeg($thumb);
Thank you for any help
Delete \t from $thumb
You can't print strings in a jpeg file. So delete echo $image; and echo $row["name"]; .
You can put this code in another file and use it as image .
index.php :
$row = mysql_fetch_array($result);
$image = $row["image"];
echo $image;
echo $row["name"];
echo '<img src="image.php">';
image.php :
$row = mysql_fetch_array($result);
$image = $row["image"];
list($width, $height) = getimagesize($image);
$newwidth = $width * 0.1;
$newheight = $height * 0.1;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($image);
imagecopyresized($thumb, $source, 0,0,0,0, $newwidth, $newheight, $width, $height);
$h = str_replace('\t','',$thumb);
header('Content-Length: '.strlen($h), true);
header("Content-type: image/jpeg");
imagejpeg($thumb);

resize image - showing only half if the pic

this script some how showing half of the image not the whole image... i am trying to figure this out what is wrong here
any help please
// The file
$ftimage = $_POST['feature_image'];
// maximum height 150 X width 150
$width = 150;
$height = 150;
// Dimensions Set
list($width_orig, $height_orig) = getimagesize($ftimage);
$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($ftimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
$image = imagejpeg($image_p, null, 100);
var_dump($image);
Try to replace it
$ftimage = $_POST['feature_image'];
with it:
$ftimage = $_POST['feature_image']['tmp_name'];
Or if in your POST request stores a URL of remote image, try something like that:
$image = imagecreatefromstring(file_get_contents($ftimage));

Categories