Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio



Create product thumbnail image from admin side in oscommerce

  • Category:
  • PHP
Create product  thumbnail image from admin side in oscommerce

Posted On : August 10, 2013

| No Comment

All online shop require product image in various size. In oscommerce we can easily create thumbnail image by following custom function.

For the create thumbnail image PHP GD library must be enable to your web server. make it sure before use this function to your oscommerce based store.

First of all Open file from this path admin/includes/functions/general.php and put  the following function at the end of file

  function resize_small($source_image, $destination, $tn_w, $tn_h, $quality = 100, $wmsource = false)
    $info = getimagesize($source_image);
	//echo $source_image;die;
    $imgtype = image_type_to_mime_type($info[2]);
    #assuming the mime type is correct
    switch ($imgtype) {
        case 'image/jpeg':
            $source = imagecreatefromjpeg($source_image);
			//print_r ($source);die;
        case 'image/gif':
            $source = imagecreatefromgif($source_image);
        case 'image/png':
            $source = imagecreatefrompng($source_image);
            die('Invalid image type.');
    #Figure out the dimensions of the image and the dimensions of the desired thumbnail
    $src_w = imagesx($source);
    $src_h = imagesy($source);
    #Do some math to figure out which way we'll need to crop the image
    #to get it proportional to the new size, then crop or adjust as needed

    $x_ratio = $tn_w / $src_w;
    $y_ratio = $tn_h / $src_h;
    if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {
        $new_w = $src_w;
        $new_h = $src_h;
    } elseif (($x_ratio * $src_h) < $tn_h) {
        $new_h = ceil($x_ratio * $src_h);
        $new_w = $tn_w;
    } else {
        $new_w = ceil($y_ratio * $src_w);
        $new_h = $tn_h;
    $newpic = imagecreatetruecolor(round($new_w), round($new_h));
    imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
    $final = imagecreatetruecolor($tn_w, $tn_h);
    $backgroundColor = imagecolorallocate($final, 255, 255, 255);
    imagefill($final, 0, 0, $backgroundColor);
    //imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);
    imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
    #if we need to add a watermark
    if ($wmsource) {
        #find out what type of image the watermark is
        $info    = getimagesize($wmsource);
        $imgtype = image_type_to_mime_type($info[2]);
        #assuming the mime type is correct
        switch ($imgtype) {
            case 'image/jpeg':
                $watermark = imagecreatefromjpeg($wmsource);
            case 'image/gif':
                $watermark = imagecreatefromgif($wmsource);
            case 'image/png':
                $watermark = imagecreatefrompng($wmsource);
                die('Invalid watermark type.');
        #if we're adding a watermark, figure out the size of the watermark
        #and then place the watermark image on the bottom right of the image
        $wm_w = imagesx($watermark);
        $wm_h = imagesy($watermark);
        imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
	  $infoo = pathinfo($destination);
		$image_name =  basename($destination,'.'.$infoo['extension']);
		$ext = end(explode('.',  $destination));
		$destination = DIR_FS_CATALOG_IMAGES.$image_name.'_small.'.$ext;
    if (imagejpeg($final, $destination, $quality)) {
				//echo $destination;die;
        return true;
    return false;

Now Open admin/categories.php file and find the following code.

$sql_data_array['products_image'] = tep_db_prepare_input($products_image->filename);

Put the following code just below it.

$products_image_name = $products_image->filename ;
resize_small(DIR_FS_CATALOG_IMAGES .$products_image_name, DIR_FS_CATALOG_IMAGES.$products_image_name, 100, 120);

now insert new product from admin and check it out image folder. you can see 2 images on it one of is original image and other thumbnail.

In the above I  width = 100px and height  = 120px . you can set your own width and height for the thumbnail image.

  • Tags: