Image Resizing
This is upload only and requires you have the gd-lib installed.
set the $size_to to the size(du) and $target to the target path. you only need to set the path up to the file name, the extension is taken care of by $extension.
PHP Code:
$size_to =100;
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$imgtype = $_FILES['new_image']['type'];
$extensionfind = strpos($imgtype, "/" );
$extensionlock = ($extensionfind + 1 );
$extension = strtolower(substr("$imgtype", $extensionlock ));
$target = "path/to/file.$extension";
move_uploaded_file($source, $target);
$file = "$target";
$save = "$target";
list($width, $height) = getimagesize($file) ;
if ($extension == "jpg" || $extension == "jpeg" || $extension == "png" || $extension == "gif")
{
$img_data_size = $_FILES['new_image']['size'];
if ($img_data_size < 8388608)
{
if ($width > $size_to)
{
$modwidth = $size_to;
}
else
{
$modwidth = $width ;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
if( $extension == "jpg" || $extension == "jpeg" ) { $image = imagecreatefromjpeg ($file); }
if( $extension == "gif" ) { $image = imagecreatefromgif ($file); }
if( $extension == "png" ) { $image = imagecreatefrompng ($file); }
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
if( $extension == "jpg" || $extension == "jpeg" ) { imagejpeg($tn, $save, 100) ; }
if( $extension == "gif" ) { imagegif($tn, $save, 100) ; }
if( $extension == "png" ) { imagepng($tn, $save, 9) ; }
}
else
{
$image_upload_error ="Images must be under: 8MB";
}
}
else
{
$image_upload_error ="Image was not an accepted format. Accepted types are jpg - jpeg - gif - png";
}
PHP Code:
if (isset($image_upload_error)){ echo "$image_upload_error"; }