Author Topic: Modifying class.img2thumb.php to "crop" images  (Read 2777 times)

coffeecore

  • Beginner
  • *
  • Posts: 1
Modifying class.img2thumb.php to "crop" images
« on: April 04, 2011, 05:34:36 AM »
Hello to everyone,

first of all i want to apologize if this was discussed before (i didn't find any similar posts)

What i wanted to achieve is to be able to change the size of the $product_full_image to any other size so i can be able to use it as a thumb, medium, big and original picture size and all different sizes to be cached

What i found out is that you can change the $product_full_image size

by changing this

Code: [Select]
echo ps_product::image_tag( $product_thumb_image, 'class="browseProductImage"
border="0" title="'.$product_name.'" alt="'.$product_name .'"' )

with this

Code: [Select]
echo ps_product::image_tag(basename($product_full_image), 'class="browseProductImage"
border="0" title="'.$product_name.'"
alt="'.$product_name .'"',1,'product',250,300 )

this works fine but the image wasn't exactly 250px,300px  and this was messing up the view

so in order to make the resizing of the image "resizing and cropping"

i looked up the class.img2thumb.php where i changed the function NewImgResize with this

Code: [Select]
function NewImgResize($orig_img,$newxsize,$newysize,$filename)
{
//$newxsize; //new width

//$newysize; //new height

$orig_size = getimagesize($filename);

$heightRatio = $orig_size[1]/$newysize;
$widthRatio = $orig_size[0]/$newxsize;

if($heightRatio < $widthRatio){
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}

$optimalHeight = $orig_size[1]/$optimalRatio;

$optimalWidth = $orig_size[0]/$optimalRatio;

$imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);

$bgfill = imagecolorallocate( $imageResized, $this->bg_red, $this->bg_green, $this->bg_blue );


imagealphablending($imageResized, false);

imagesavealpha($imageResized,true);

$transparent = imagecolorallocatealpha($imageResized, 255, 255, 255, 127);

imagecopyresampled($imageResized, $orig_img, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $orig_size[0], $orig_size[1]);


$cropStartX = ( $optimalWidth / 2) - ( $newxsize /2 );

$cropStartY = ( $optimalHeight/ 2) - ( $newysize/2 );

$crop = $imageResized;

$imageResized = imagecreatetruecolor($newxsize , $newysize);


$bgfill = imagecolorallocate( $imageResized, $this->bg_red, $this->bg_green, $this->bg_blue );
 
imagealphablending($imageResized, false);
 
imagesavealpha($imageResized,true);
 
$transparent = imagecolorallocatealpha($imageResized, 255, 255, 255, 127);
 

imagecopyresampled($imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newxsize, $newysize , $newxsize, $newysize);

return $imageResized;
}

and in show_image_in_imgtag.php i deleted this part

Code: [Select]
//if( $newxsize < $newysize ) {
// Don't let $newxsize be smaller than 55% of $newysize
// $newxsize = max( $newxsize, 0.55 * $newysize );
//}
//elseif( $newysize < $newxsize ) {
// Don't let $newysize be smaller than 55% of $newxsize
// $newysize = max( $newysize, 0.55 * $newxsize );
//}

so there are no restrictions about the dimensions of the image

and it works great (just make sure to delete the previously cached images
in products/resized/) so it can make new ones :)

So now i am stuck on other problem that i cant understand

i wanted to use this in one of my browse templates and in the flypage

so in the browse_5.php

i just used

Code: [Select]
echo ps_product::image_tag(basename($product_full_image), 'class="browseProductImage" border="0" title="'.$product_name.'" alt="'.$product_name .'"',1,'product',190,190 );

and it made perfect 190 by 190 thumbs

but in the flypage.tpl.php where i did

Code: [Select]
echo ps_product::image_tag(basename($product_full_image), 'class="main_img" border="0" title="'.$product_name.'" alt="'.$product_name .'"',1,'product',430,430 );

it shows the same picture that the previous script did (the one 190px by 190px)

and when i go to

Code: [Select]

localhost/components/com_virtuemart/show_image_in_imgtag.php?filename=image.png&newxsize=430&newysize=430&fileout=


the image is resized and cached then

but not where i run this from the flypage

i think the problem is in the show_image_in_imgtag.php where the variables

$fileout and $filename2 are checked

Code: [Select]

if( $resize_image ) {
if( file_exists($filename2)) {
$fileout = $filename2;
} else {
$fileout = dirname( $filename2 ) .'/'.$file."_".$newxsize."x".$newysize.$noimgif.$ext;
}
} else {
$fileout = $filename;
}

I'm stuck and I'll appreciate your help!


Best wishes, coffeecore

pixeltopaper

  • Beginner
  • *
  • Posts: 6
Re: Modifying class.img2thumb.php to "crop" images
« Reply #1 on: September 08, 2011, 21:00:47 PM »
I can't help you but just want to let you know you have saved me hours of work doing this myself, thanks

pixeltopaper

  • Beginner
  • *
  • Posts: 6
Re: Modifying class.img2thumb.php to "crop" images
« Reply #2 on: September 08, 2011, 21:53:53 PM »
ok so i have figured out how to set your own cropped image for the product description page

in administrator/components/com_virtuemart/classes/ps_product

about line 1413 there is:

Code: [Select]
if(PSHOP_IMG_RESIZE_ENABLE == '1' && $resize==1) {

$url = $mosConfig_live_site."/components/com_virtuemart/show_image_in_imgtag.php?filename=".urlencode($image)."&amp;newxsize=".PSHOP_IMG_WIDTH."&amp;newysize=".PSHOP_IMG_HEIGHT."&amp;fileout=";
if( !strpos( $args, "height=" )) {
$arr = @getimagesize( vmImageTools::getresizedfilename( $image, $path_appendix, '', $thumb_height, $thumb_width ) );
$width = $arr[0]; $height = $arr[1];
}
}

replace with:

Code: [Select]
if(PSHOP_IMG_RESIZE_ENABLE == '1' && $resize==1) {
if($thumb_width == 0 && $thumb_height == 0){
$url = $mosConfig_live_site."/components/com_virtuemart/show_image_in_imgtag.php?filename=".urlencode($image)."&amp;newxsize=".PSHOP_IMG_WIDTH."&amp;newysize=".PSHOP_IMG_HEIGHT."&amp;fileout=";
}else{
$url = $mosConfig_live_site."/components/com_virtuemart/show_image_in_imgtag.php?filename=".urlencode($image)."&amp;newxsize=".$thumb_width."&amp;newysize=".$thumb_height."&amp;fileout=";
}
if( !strpos( $args, "height=" )) {
$arr = @getimagesize( vmImageTools::getresizedfilename( $image, $path_appendix, '', $thumb_height, $thumb_width ) );
$width = $arr[0]; $height = $arr[1];
}
}

and on your flypage use

Code: [Select]
<a class="prodmainimage" href="<?php echo 'components/com_virtuemart/shop_image/product/'.basename($product_full_image?>" rel="lightbox[<?php echo 'product'.$product_id ?>]"><?php echo ps_product::image_tag(basename($product_full_image), 'title="'.$product_name.'" alt="'.$product_name .'"',1,'product',345,301 );
 
?>
</a>

to insert your newly cropped image, you can remove the <a> if you don't want the lightbox to load up. the two last numbers should be the width and height you want the new image to be.

hope this helps someone

vannquish

  • Full Member
  • ***
  • Posts: 275
    • Midlandsfc
Re: Modifying class.img2thumb.php to "crop" images
« Reply #3 on: June 28, 2012, 04:46:02 AM »
this is great, thanks!

VirtueMart Forum

Re: Modifying class.img2thumb.php to "crop" images
« Reply #3 on: June 28, 2012, 04:46:02 AM »