/** * 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 * * Note: checked for code removal in 5500 (ISC-102) but appears to be in use by variations -ge * * @param string $ImageName * @param string $Size * @param bool $OverrideExisting * @return bool */ public function _AutoGenerateThumb($ImageName, $Size="thumb", $OverrideExisting=false) { $imgFile = realpath(ISC_BASE_PATH."/" . GetConfig('ImageDirectory')); $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 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 ISC_IMAGE_LIBRARY_FACTORY::setImageFileMemLimit($imgFile); $thumbFile = realpath(ISC_BASE_PATH."/" . GetConfig('ImageDirectory')); $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); if($Size == "tiny") { $AutoThumbSize = ISC_PRODUCT_IMAGE_SIZE_TINY; } else { $AutoThumbSize = ISC_PRODUCT_IMAGE_SIZE_THUMBNAIL; } // This thumbnail is smaller than the Unreal 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; }
private function SaveCategoryImage() { if (!array_key_exists('catimagefile', $_FILES) || $_FILES['catimagefile']['error'] !== 0 || strtolower(substr($_FILES['catimagefile']['type'], 0, 6)) !== 'image/') { return false; } // Attempt to set the memory limit so we can resize this image ISC_IMAGE_LIBRARY_FACTORY::setImageFileMemLimit($_FILES['catimagefile']['tmp_name']); // Determine the destination directory $randomDir = strtolower(chr(rand(65, 90))); $destPath = realpath(ISC_BASE_PATH.'/' . GetConfig('ImageDirectory')); if (!is_dir($destPath . '/' . $randomDir)) { if (!isc_mkdir($destPath . '/' . $randomDir)) { $randomDir = ''; } } $destFile = GenRandFileName($_FILES['catimagefile']['name'], 'category'); $destPath = $destPath . '/' . $randomDir . '/' . $destFile; $returnPath = $randomDir . '/' . $destFile; $tmp = explode('.', $_FILES['catimagefile']['name']); $ext = strtolower($tmp[count($tmp)-1]); if ($ext == 'jpg') { $srcImg = imagecreatefromjpeg($_FILES['catimagefile']['tmp_name']); } else if($ext == 'gif') { $srcImg = imagecreatefromgif($_FILES['catimagefile']['tmp_name']); if(!function_exists('imagegif')) { $gifHack = 1; } } else { $srcImg = imagecreatefrompng($_FILES['catimagefile']['tmp_name']); } $srcWidth = imagesx($srcImg); $srcHeight = imagesy($srcImg); $widthLimit = GetConfig('CategoryImageWidth'); $heightLimit = GetConfig('CategoryImageHeight'); // If the image is small enough, simply move it if($srcWidth <= $widthLimit && $srcHeight <= $heightLimit) { imagedestroy($srcImg); move_uploaded_file($_FILES['catimagefile']['tmp_name'], $destPath); // set image to be writable isc_chmod($destPath, ISC_WRITEABLE_FILE_PERM); return $returnPath; } // Otherwise, resize it $attribs = getimagesize($_FILES['catimagefile']['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['catimagefile']['tmp_name']); // Change the permissions on the thumbnail file isc_chmod($destPath, ISC_WRITEABLE_FILE_PERM); return $returnPath; }
/** * Load the current file from disk to in-memory resource * * @return void */ public function loadImageFileToScratch() { $filePath = $this->getFilePath(); $imageType = $this->getImageType(); // Attempt to increase the memory limit before loading in the image, to ensure it'll fit in memory ISC_IMAGE_LIBRARY_FACTORY::setImageFileMemLimit($filePath); switch ($imageType) { case IMAGETYPE_GIF: $this->_scratchResource = @imagecreatefromgif($filePath); if ($this->getScratchResource()) { imagecolortransparent($this->getScratchResource()); } break; case IMAGETYPE_PNG: $this->_scratchResource = @imagecreatefrompng($filePath); if ($this->_scratchResource) { // this sets up alpha transparency support when manipulating and saving the in-memory image imagealphablending($this->getScratchResource(), false); imagesavealpha($this->getScratchResource(), true); } break; case IMAGETYPE_JPEG: $this->_scratchResource = @imagecreatefromjpeg($filePath); break; default: throw new ISC_IMAGE_LIBRARY_GD_UNSUPPORTEDIMAGETYPE_EXCEPTION($imageType); } $this->_updateImageInformation(true); if (!$this->getScratchResource()) { throw new ISC_IMAGE_LIBRARY_GD_IMAGECREATEFROMFILE_EXCEPTION($imageType, $filePath); } }