private function SaveQValueAssociationImage() { if (!array_key_exists('associationimage', $_FILES) || $_FILES['associationimage']['error'] !== 0 || strtolower(substr($_FILES['associationimage']['type'], 0, 6)) !== 'image/') { return false; } // Attempt to set the memory limit setImageFileMemLimit($_FILES['associationimage']['tmp_name']); // Generate the destination path $randomDir = strtolower(chr(rand(65, 90))); $destPath = realpath(ISC_BASE_PATH . '/' . GetConfig('ImageDirectory')); if (!is_dir($destPath . '/' . $randomDir)) { if (!@mkdir($destPath . '/' . $randomDir, 0777)) { $randomDir = ''; } } $destFile = GenRandFileName($_FILES['associationimage']['name'], 'category'); $destPath = $destPath . '/' . $randomDir . '/' . $destFile; $returnPath = $randomDir . '/' . $destFile; $tmp = explode('.', $_FILES['associationimage']['name']); $ext = strtolower($tmp[count($tmp) - 1]); if ($ext == 'jpg') { $srcImg = imagecreatefromjpeg($_FILES['associationimage']['tmp_name']); } else { if ($ext == 'gif') { $srcImg = imagecreatefromgif($_FILES['associationimage']['tmp_name']); if (!function_exists('imagegif')) { $gifHack = 1; } } else { $srcImg = imagecreatefrompng($_FILES['associationimage']['tmp_name']); } } $srcWidth = imagesx($srcImg); $srcHeight = imagesy($srcImg); $widthLimit = GetConfig('BrandImageWidth'); $heightLimit = GetConfig('BrandImageHeight'); // If the image is small enough, simply move it and leave it as is if ($srcWidth <= $widthLimit && $srcHeight <= $heightLimit) { imagedestroy($srcImg); move_uploaded_file($_FILES['associationimage']['tmp_name'], $destPath); return $returnPath; } // Otherwise, the image needs to be resized $attribs = getimagesize($_FILES['associationimage']['tmp_name']); $width = $attribs[0]; $height = $attribs[1]; if ($width > $widthLimit) { $height = ceil($widthLimit / $width * $height); $width = $widthLimit; } if ($height > $heightLimit) { $width = ceil($heightLimit / $height * $width); $height = $heightLimit; } $dstImg = imagecreatetruecolor($width, $height); if ($ext == "gif" && !isset($gifHack)) { $colorTransparent = imagecolortransparent($srcImg); imagepalettecopy($srcImg, $dstImg); imagecolortransparent($dstImg, $colorTransparent); imagetruecolortopalette($dstImg, true, 256); } else { if ($ext == "png") { ImageColorTransparent($dstImg, ImageColorAllocate($dstImg, 0, 0, 0)); ImageAlphaBlending($dstImg, false); } } imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); if ($ext == "jpg") { imagejpeg($dstImg, $destPath, 100); } else { if ($ext == "gif") { if (isset($gifHack) && $gifHack == true) { $thumbFile = isc_substr($destPath, 0, -3) . "jpg"; imagejpeg($dstImg, $destPath, 100); } else { imagegif($dstImg, $destPath); } } else { imagepng($dstImg, $destPath); } } @imagedestroy($dstImg); @imagedestroy($srcImg); @unlink($_FILES['associationimage']['tmp_name']); // Change the permissions on the thumbnail file isc_chmod($returnPath, ISC_WRITEABLE_FILE_PERM); return $returnPath; }
public function _AutoGenerateInsThumb($ImageName, $Size = "thumb", $OverrideExisting = false) { // Takes the filename of an image already uploaded into the // image directory, generates a thumbnal from it, stores it // in the image directory and returns its name $imgFile = realpath(ISC_BASE_PATH . "/install_images"); $imgFile .= "/" . $ImageName; if ($ImageName == '' || !file_exists($imgFile)) { return false; } // A list of thumbnails too $tmp = explode(".", $imgFile); $ext = isc_strtolower($tmp[count($tmp) - 1]); // If overriding the existing image, set the output filename to the input filename //Large and medium size images by Simha if ($Size == 'large') { $thumbFileName = $ImageName; } else { if ($Size == 'medium') { $thumbFileName = GenRandFileName($ImageName, $Size); } else { if ($OverrideExisting == true) { $thumbFileName = $ImageName; } else { $thumbFileName = GenRandFileName($ImageName, $Size); } } } $attribs = @getimagesize($imgFile); $width = $attribs[0]; $height = $attribs[1]; if (!is_array($attribs)) { return false; } // Check if we have enough available memory to create this image - if we don't, attempt to bump it up setImageFileMemLimit($imgFile); $thumbFile = realpath(ISC_BASE_PATH . "/install_images"); $thumbFile .= "/" . $thumbFileName; if ($ext == "jpg") { $srcImg = @imagecreatefromjpeg($imgFile); } else { if ($ext == "gif") { $srcImg = @imagecreatefromgif($imgFile); if (!function_exists("imagegif")) { $gifHack = 1; } } else { $srcImg = @imagecreatefrompng($imgFile); } } if (!$srcImg) { return false; } $srcWidth = @imagesx($srcImg); $srcHeight = @imagesy($srcImg); //Large and medium size images by Simha if ($Size == 'large') { $AutoThumbSize = 800; } else { if ($Size == 'medium') { $AutoThumbSize = 70; } else { if ($Size == "tiny") { $AutoThumbSize = ISC_TINY_THUMB_SIZE; } else { $AutoThumbSize = GetConfig('AutoThumbSize'); } } } // This thumbnail is smaller than the Interspire Shopping Cart dimensions, simply copy the image and return if ($srcWidth <= $AutoThumbSize && $srcHeight <= $AutoThumbSize) { @imagedestroy($srcImg); if ($OverrideExisting == false) { @copy($imgFile, $thumbFile); } return $thumbFileName; } // Make sure the thumb has a constant height $thumbWidth = $width; $thumbHeight = $height; if ($width > $AutoThumbSize) { $thumbWidth = $AutoThumbSize; $thumbHeight = ceil($height * ($AutoThumbSize * 100 / $width) / 100); $height = $thumbHeight; $width = $thumbWidth; } if ($height > $AutoThumbSize) { $thumbHeight = $AutoThumbSize; $thumbWidth = ceil($width * ($AutoThumbSize * 100 / $height) / 100); } $thumbImage = @imagecreatetruecolor($thumbWidth, $thumbHeight); if ($ext == "gif" && !isset($gifHack)) { $colorTransparent = @imagecolortransparent($srcImg); @imagepalettecopy($srcImg, $thumbImage); @imagecolortransparent($thumbImage, $colorTransparent); @imagetruecolortopalette($thumbImage, true, 256); } else { if ($ext == "png") { @ImageColorTransparent($thumbImage, @ImageColorAllocate($thumbImage, 0, 0, 0)); @ImageAlphaBlending($thumbImage, false); } } @imagecopyresampled($thumbImage, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight); if ($ext == "jpg") { @imagejpeg($thumbImage, $thumbFile, 100); } else { if ($ext == "gif") { if (isset($gifHack) && $gifHack == true) { $thumbFile = isc_substr($thumbFile, 0, -3) . "jpg"; @imagejpeg($thumbImage, $thumbFile, 100); } else { @imagegif($thumbImage, $thumbFile); } } else { @imagepng($thumbImage, $thumbFile); } } @imagedestroy($thumbImage); @imagedestroy($srcImg); // Change the permissions on the thumbnail file isc_chmod($thumbFile, ISC_WRITEABLE_FILE_PERM); return $thumbFileName; }