100+ |
i have an image which width is 213 and height is 200 when
i echo the image and i resize it
echo "<img src='company/$present' width='70' height='68'/>";
the image was not as clear as when it was 213 * 200.how can i
make the image clear after i have resize it to 70 * 200.
| |
Share:
Expert 4TB |
Hi.
When you resize it as you do, with the <img> height and width attributes, you are essentially leaving it up to the browser to resize it.
Predictably, Internet Explorer does a horrible job at this, while the other major browsers do a pretty good job resizing them.
If you want to ensure that the image is scaled smoothly, you can have PHP resize the image. The GD extension offers the imagecopyresampled function, which does an excellent job resizing images. (See the documentation for examples on how to use it)
Note that image processing is a very resource intensive task, so if you frequently display you images resized, you should save the resized image to the server's file-system and serve it from there, rather then resize it for every request.
| | 100+ |
what if i want to echo the image from my database, which
will involve vaious image how can i then resize it and
also keep the resize image smooth like the original.i
will appreciate it if the answer that will be given to me will
be edited on my code -
<?php
-
$query = "SELECT * FROM photo";
-
$result = mysql_query ($query) or die('query error');
-
$count = 0;
-
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
-
$image = $line[picname];
-
echo "<img src='company/$image'/> ";
-
$count++;
-
}
-
| | Expert Mod 8TB |
if you want to echo the image from DB (that is, you have the image itself stored there (as binary data)) you need a script of its own that fetches the image, applies any changes you want (e.g. resizing) and gives out the result.
| | 512MB |
This article about Uploading files into a MySQL database using PHP
might help you. It discusses a great way to handle image files, although it might not directly answer your "resizing" question. Just throwing it out there as a reference.
Thanks Atli ;)
| | Expert 4TB |
Resizing images 'on the fly' (whenever you pull them from a database, etc) will eat into your resources like a fat kid into some delicious cake. When a photo is uploaded, use the GD extension that Atli linked to, and then save a seperate copy of the resized image. That way, you're saving resources, but sacrificing a small amount of HDD space.
| | 100+ |
i have a problem with the following code below, the image is not showing
the image is display as if there is a missing link, i have check the code
and i see not thing wrong with it and aslo i have enable the GD Library on
my wamp server -
<?php
-
//my Image
-
$imgSrc = '36.jpg';
-
list($width, $height) = getimagesize($imgSrc);
-
//saving the image into memory (for manipulation with GD Library)
-
$myImage = imagecreatefromjpeg($imgSrc);
-
if($width > $height) $biggestSide = $width;
-
else $biggestSide = $height;
-
$cropPercent = .5;
-
$cropWidth = $biggestSide*$cropPercent;
-
$cropHeight = $biggestSide*$cropPercent;
-
$c1 = array("x"=>($width-$cropWidth)/2, "y"=>($height-$cropHeight)/2);
-
$thumbSize = 60;
-
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
-
imagecopyresampled($thumb, $myImage, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight);
-
$lineWidth = 1;
-
$margin = 0;
-
$green = imagecolorallocate($thumb, 193, 252, 182);
-
-
for($i=0; $i<2; $i++){
-
imagefilledrectangle($thumb, $margin, $margin, $margin+$lineWidth, $thumbSize-$margin, $green);
-
imagefilledrectangle($thumb, $thumbSize-$margin-$lineWidth, $margin, $thumbSize-$margin, $thumbSize-$margin, $green);
-
imagefilledrectangle($thumb, $margin, $margin, $thumbSize-$margin-$lineWidth, $margin+$lineWidth, $green);
-
imagefilledrectangle($thumb, $margin, $thumbSize-$margin-$lineWidth, $thumbSize-$margin-$lineWidth, $thumbSize-$margin,$green);
-
$margin+=4;
-
}
-
header('Content-type: image/jpeg');
-
imagejpeg($thumb);
-
imagedestroy($thumb);
-
-
?>
| | Expert 1GB |
Line 7, 8 should have {curly brackets}: - if($width > $height) {$biggestSide = $width;}
-
else {$biggestSide = $height;}
Other than that I can't see anything big... Will look again later if noone has replied.
| | Expert 4TB | @TheServant
Not true in this case; because the conditional statement only has one statement within it, curly brackets can be omitted. -
// will work
-
if ( $a === $b )
-
echo "hi";
-
else
-
echo "yo";
-
-
// won't work
-
if ( $a === $b )
-
echo "hello ";
-
echo "world";
-
else
-
echo "hi";
-
| | Expert 1GB |
Interesting. I had a feeling that was the case, but wasn't 100% sure. Anyway, I use them all the time for consistency. Thanks for the tip. Suggestion withdrawn.
| | 100+ |
i have made the neccessary adjustment to line 7 and 8 of the code... but the image is still not displaying as told.
| | 100+ |
is there any thing else i can do to make the image display
| | Expert 4TB | @simon2x1
Have you turned on debugging messages? What does happen? What doesn't happen.
The more info you give us, the better.
| | 100+ |
i have visited the link (imagecopyresample) you gave to me and i try almost
every code on the link non gave me an error but the image still did not display
like the code below is met to resize an image but the image did not display.
i have turn on my Gd library on my wamp server, the image i want to resize is
in the directory with the page so what is wrong and what should i do -
<?php
-
// define image to resize
-
$file = "Aizen.jpg";
-
// create object from original image
-
$imOrig = imagecreatefromjpeg($file);
-
list($width, $height) = getimagesize($file);
-
$imNew = imagecreatetruecolor($width * 1.5, $height * 1.5);
-
// resample and resize old image
-
imagecopyresampled($imNew, $imOrig, 0, 0, 0, 0, $width * 1.5,
-
$height * 1.5, $width, $height);
-
// output resized image
-
header("Content-type: image/jpeg");
-
imagejpeg($imNew);
-
imagedestroy($im);
-
?>
-
| | 100+ |
How can I resize an image whenever the image is being uploaded directly to a folder?
| | Expert 4TB | @simon2x1
What do you mean by 'uploaded directly to a folder'? Is a PHP script handling the upload? Or are we speaking of FTP, or whatever?
Don't be lazy when asking a question.
- mark.
| | 100+ |
sorry i mean a php script handling the upload that automatically resize an image we a person upload an image
| | Expert 1GB | @simon2x1
To do anything with an image, it must first be wholly downloaded to the server. After that PHP (or any other program on the server) can manipulated.
Checkout GD, Imagick() libraries at php.net. After you've got access to these image libraries resizing will be trivial. After resize you can save it anywhere PHP/Apache user has permission to on the filesystem.
Dan
| | 256MB |
save this code as SimpleImage.php -
<title>: : : members online resource centre</title><?php
-
/*
-
* File: SimpleImage.php
-
* Author: Simon Jarvis
-
* Copyright: 2006 Simon Jarvis
-
* Date: 08/11/06
-
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
-
*
-
* This program is free software; you can redistribute it and/or
-
* modify it under the terms of the GNU General Public License
-
* as published by the Free Software Foundation; either version 2
-
* of the License, or (at your option) any later version.
-
*
-
* This program is distributed in the hope that it will be useful,
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
* GNU General Public License for more details:
-
* http://www.gnu.org/licenses/gpl.html
-
*
-
*/
-
-
class SimpleImage {
-
-
var $image;
-
var $image_type;
-
-
function load($filename) {
-
$image_info = getimagesize($filename);
-
$this->image_type = $image_info[2];
-
if( $this->image_type == IMAGETYPE_JPEG ) {
-
$this->image = imagecreatefromjpeg($filename);
-
} elseif( $this->image_type == IMAGETYPE_GIF ) {
-
$this->image = imagecreatefromgif($filename);
-
} elseif( $this->image_type == IMAGETYPE_PNG ) {
-
$this->image = imagecreatefrompng($filename);
-
}
-
}
-
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
-
if( $image_type == IMAGETYPE_JPEG ) {
-
imagejpeg($this->image,$filename,$compression);
-
} elseif( $image_type == IMAGETYPE_GIF ) {
-
imagegif($this->image,$filename);
-
} elseif( $image_type == IMAGETYPE_PNG ) {
-
imagepng($this->image,$filename);
-
}
-
if( $permissions != null) {
-
chmod($filename,$permissions);
-
}
-
}
-
function output($image_type=IMAGETYPE_JPEG) {
-
if( $image_type == IMAGETYPE_JPEG ) {
-
imagejpeg($this->image);
-
} elseif( $image_type == IMAGETYPE_GIF ) {
-
imagegif($this->image);
-
} elseif( $image_type == IMAGETYPE_PNG ) {
-
imagepng($this->image);
-
}
-
}
-
function getWidth() {
-
return imagesx($this->image);
-
}
-
function getHeight() {
-
return imagesy($this->image);
-
}
-
function resizeToHeight($height) {
-
$ratio = $height / $this->getHeight();
-
$width = $this->getWidth() * $ratio;
-
$this->resize($width,$height);
-
}
-
function resizeToWidth($width) {
-
$ratio = $width / $this->getWidth();
-
$height = $this->getheight() * $ratio;
-
$this->resize($width,$height);
-
}
-
function scale($scale) {
-
$width = $this->getWidth() * $scale/100;
-
$height = $this->getheight() * $scale/100;
-
$this->resize($width,$height);
-
}
-
function resize($width,$height) {
-
$new_image = imagecreatetruecolor($width, $height);
-
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
-
$this->image = $new_image;
-
}
-
}
-
?>
-
-
-
-
use this code to help you upload/resize the images -
include('SimpleImage.php');
-
while(list($key,$value) = each($_FILES['images']['name']))
-
{
-
if(!empty($value))
-
{
-
$filename = $value;
-
$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
-
-
$add = "your/directory/$filename";
-
//echo $_FILES['images']['type'][$key];
-
// echo "<br>";
-
copy($_FILES['images']['tmp_name'][$key], $add);
-
chmod("$add",0777);
-
-
}
-
$image = new SimpleImage();
-
$image->load("$add");
-
$image->resize(546,403);
-
$image->save("$add");
-
-
}
-
to change the size you wish the images to be you edit this part of the above code
$image = new SimpleImage();
$image->load("$add");
$image->resize(546,403);
$image->save("$add");
the 1st digit (546) being the width and the 2nd digit (403) being the height.....change them as needed.
make sure that the name values on your upload form is set for an array.....name all fields images[].....see example form below -
<form name="the_form" id="the_form" method="POST" action="yourfile.php" enctype="multipart/form-data">
-
1. <input name="images[]" type="file" class="imgfield" id="field_1"
-
2. <input name="images[]" type="file" class="imgfield" id="field_2"
-
3. <input name="images[]" type="file" class="imgfield" id="field_3"
-
4. <input name="images[]" type="file" class="imgfield" id="field_4"
-
5. <input name="images[]" type="file" class="imgfield" id="field_5"
-
<input name="count" type="hidden" id="count" value="4"/>
-
-
<input name="Submit" type="submit" value="Upload Image" />
-
</form>
-
hope this helps
| | 256MB |
also you dont need to change any part of that coding only the size you want the image to be and the names on your forms
| | 100+ |
the code worked, it reduced the image which i uploaded thanks for that but when i clicked submit without uploading the image it gave me error that look
like unreadable word so i put the folowing if($_FILES['images']['size'] == 0)
between line 4 and 5 it gave me the same error put it between line 14 and 15 the same error please i dont want the upload feild to post a blank field and also not to allow any image which is not gif, jpeg and png how can you help i need this for my project -
<?php
-
include('SimpleImage.php');
-
if (isset($_POST['submit'])) {
-
while(list($key,$value) = each($_FILES['images']['name'])){
-
if(!empty($value))
-
{
-
$filename = $value;
-
$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
-
$add = "./collect/$filename";
-
echo $_FILES['images']['type'][$key];
-
echo "<br>";
-
copy($_FILES['images']['tmp_name'][$key], $add);
-
chmod("$add",0777);
-
}
-
$image = new SimpleImage();
-
$image->load("$add");
-
$image->resize(100,100);
-
$image->save("$add");
-
-
}
-
}
-
?>
-
<form name="the_form" id="the_form" method="POST" action="home.php" enctype="multipart/form-data">
-
<input name="images[]" type="file" id="field_1" >
-
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
-
<input name="submit" type="submit" value="submit" />
-
</form>
-
| | 100+ |
the code worked, it reduced the image which i uploaded thanks for that but when i clicked submit without uploading the image it gave me error that look
like unreadable word so i put the folowing if($_FILES['images']['size'] == 0)
between line 4 and 5 it gave me the same error put it between line 14 and 15 the same error please i dont want the upload feild to post a blank field and also not to allow any image which is not gif, jpeg and png how can you help i need this for my project -
<?php
-
include('SimpleImage.php');
-
if (isset($_POST['submit'])) {
-
while(list($key,$value) = each($_FILES['images']['name'])){
-
if(!empty($value))
-
{
-
$filename = $value;
-
$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
-
$add = "./collect/$filename";
-
echo $_FILES['images']['type'][$key];
-
echo "<br>";
-
copy($_FILES['images']['tmp_name'][$key], $add);
-
chmod("$add",0777);
-
}
-
$image = new SimpleImage();
-
$image->load("$add");
-
$image->resize(100,100);
-
$image->save("$add");
-
-
}
-
}
-
?>
-
<form name="the_form" id="the_form" method="POST" action="home.php" enctype="multipart/form-data">
-
<input name="images[]" type="file" id="field_1" >
-
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
-
<input name="submit" type="submit" value="submit" />
-
</form>
-
-
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
8 posts
views
Thread by Chris Dewin |
last post: by
|
2 posts
views
Thread by Carl Gilbert |
last post: by
|
15 posts
views
Thread by David Lozzi |
last post: by
|
1 post
views
Thread by Arjen |
last post: by
|
2 posts
views
Thread by Tim Arnold |
last post: by
|
2 posts
views
Thread by Dominic Vella |
last post: by
|
3 posts
views
Thread by danielatdaveschool@gmail.com |
last post: by
| | | | | | | | | | | | |