PHP generated thumbnail function

Source: Internet
Author: User
Customer upload the image size is not standard, want to let the picture unified into 150x113 size. From the Internet to search the program in IE with normal, to the other browser (such as Sogou browser, etc.) is not, what is the reason? (feeling is always not executed)
The code is as follows:

Session_Start ();
if (Isset ($_session[' AdminName ')) && isset ($_session[' AdminPass ']) {


//****************************************
Generate Thumbnails ========================================
$myname =date ("Ymdhis");

$filename = "product/min/". $myname; Small picture file name
$resizewidth = 150; Create the width of the picture
$resizeheight = 113; Height of the resulting picture


function Resizeimage ($im, $maxwidth, $maxheight, $name) {
$resizewidth = $resizeheight = false;

$width = Imagesx ($im);
$height = Imagesy ($im);
if ($maxwidth && $width > $maxwidth) | | ($maxheight && $height > $maxheight)) {
if ($maxwidth && $width > $maxwidth) {
$widthratio = $maxwidth/$width;
$resizewidth =true;
}
if ($maxheight && $height > $maxheight) {
$heightratio = $maxheight/$height;
$resizeheight =true;
}
if ($resizewidth && $resizeheight) {
if ($widthratio < $heightratio) {
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif ($resizewidth) {
$ratio = $widthratio;
}elseif ($resizeheight) {
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if (function_exists ("imagecopyresampled")) {
$newim = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = Imagecreate ($newwidth, $newheight);
Imagecopyresized ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
Imagejpeg ($newim, $name. ". jpg");
Imagedestroy ($newim);
}else{
Imagejpeg ($im, $name. ". jpg");
}
}



if (isset ($_files[' image ' [' size '])) {
if ($_files[' image ' [' type '] = = "Image/pjpeg") {
$im = imagecreatefromjpeg ($_files[' image ' [' tmp_name ']);
}elseif ($_files[' image ' [' type '] = = "Image/x-png") {
$im = imagecreatefrompng ($_files[' image ' [' tmp_name ']);
}elseif ($_files[' image ' [' type '] = = "Image/gif") {
$im = imagecreatefromgif ($_files[' image ' [' tmp_name ']);
}


if ($im) {
if (File_exists ("$filename. jpg")) {
Unlink ("$filename. jpg");
}

Resizeimage ($im, $resizewidth, $resizeheight, $filename); Generate a small image
Imagedestroy ($im);

echo "Upload successful, image filename is:";
echo $myname. " JPG ";
}
}

//****************************************
}else{
echo "";
}
?>











Reply to discussion (solution)

You see if there's a generated picture in the product/min/directory.

ie, firefox,chrome try, these three lines say the code is not a problem.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.