Menu
I just now saving the image to database. But how to resize the image before save to database?
Thanks
[code=php]//If the form has been submitted
if(isset($_POST['Submit'])){
//Assign the paths needed to variables for use in the script
$filePathFull = "assets/imgf/".$_FILES["imagefile"]["name"];
$filePathThumb = "assets/imgt/";
//Assign the files TMP name and TYPE to variable for use in the script
$tmpName = $_FILES["imagefile"]["tmp_name"];
$imageType = $_FILES["imagefile"]["type"];
//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case $imageType == "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/jpeg":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/png":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/x-png":
move_uploaded_file($tmpName,$filePathFull);
break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
// Get information about the image
list($srcWidth, $srcHeight, $type, $attr) = getimagesize($filePathFull);
$origRatio = $srcWidth/$srcHeight;
//Create the correct file type based on imagetype
switch($type) {
case IMAGETYPE_JPEG:
$startImage = imagecreatefromjpeg($filePathFull);
break;
case IMAGETYPE_PNG:
$startImage = imagecreatefrompng($filePathFull);
break;
case IMAGETYPE_GIF:
$startImage = imagecreatefromgif($filePathFull);
break;
default:
return false;
}
//Get the image to create thumbnail from
$startImage;
$width = 200;
$height = 200;
if ($width/$height > $origRatio) {
$width = $height*$origRatio;
}else{
$height = $width/$origRatio;
}
//Create the thumbnail with true colours
$thumbImage = imagecreatetruecolor($width, $height);
//Generate the resized image image
imagecopyresampled($thumbImage, $startImage, 0,0,0,0, $width, $height, $srcWidth, $srcHeight);
//Generate a random number to append the filename.
$ran = "thumb_".rand () ;
$thumb2 = $ran.".jpg";
//global $thumb_Add_thumb;
//Create the path to store the thumbnail in.
$thumb_Add_thumb = $filePathThumb;
$thumb_Add_thumb .= $thumb2;
//Create the new image and put it into the thumbnails folder.
imagejpeg($thumbImage, "" .$filePathThumb. "$thumb2");
}
?>[/code]
[code=php]<?php
INSERT CONNECTION DETAILS TO DATABASE
$sql ="SELECT col_that_holds_imgthumb_path FROM table_name WHERE 1";
$query = mysql_query($sql);
$result = mysql_num_rows($query);
if($result == 0){
echo "No images in the DB";
}else{
while($row = mysql_fetch_object($query)){
echo "<img src='$row->col_that_holds_imgthumb_path' />";
}
}
?>[/code]
[code=php]
//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case $imageType == "image/gif":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/jpeg":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/pjpeg":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/png":
move_uploaded_file($tmpName,$filePathFull);
break;
case $imageType == "image/x-png":
move_uploaded_file($tmpName,$filePathFull);
break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
[/code]
[code=php]
//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
case "image/gif":
case "image/jpeg":
case "image/pjpeg":
case "image/png":
case "image/x-png":
move_uploaded_file($tmpName,$filePathFull);
break;
default:
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
[/code]
[code=php]
$valid = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
if (in_array($imageType, $valid) {
move_uploaded_file($tmpName,$filePathFull);
} else {
echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}
[/code]
[code=php]
$width = 200;
$height = 200;
if ($width/$height > $origRatio) {
$width = $height*$origRatio;
}else{
$height = $width/$origRatio;
}
[/code]
http://www.webgeek.eu/?l=/en/archive/18
This is a JavaScript that does what you want. Just translate it to PHP (Takes about 1 minute).[/QUOTE]
[code=php]<?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);
?>[/code]
0.1.9 — BETA 5.13